Jump to content

Hi,

I'm currently run into an issue with my below python code
 

import socket
import threading
import sys

class Server:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        connections = []
        def __init__(self):
                self.sock.bind(('0.0.0.0', 10000))
                self.sock.listen(1)

        def handler(self, c, a):
                while True:
                        data = c.recv(1024)
                        for connection in self.connections:
                                connection.send(data)
                        if not data:
                                print(str(a[0]) + ':' + str(a[1]), "disconnected")
                                self.connections.remove(c)
                                c.close()
                                break
        def run(self):
                while True:
                        c, a = self.sock.accept()
                        cThread = threading.Thread(target=self.handler, args=(c, a))
                        cThread.daemon = True
                        cThread.start()
                        self.connections.append(c)
                        print(str(a[0]) + ':' + str(a[1]), "connected")

class Client:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        def sendMsg(self):
                while True:
                        self.sock.send(bytes(input(""), 'utf-8'))

        def __init__(self, address):
                self.sock.connect((address, 10000))

                iThread = threading.Thread(target=self.sendMsg)
                iThread.daemon = True
                iThread.start()

                while True:
                        data = self.sock.recv(1024)
                        if not data:
                                break
                        print(str(data, 'utf-8'))

if (len(sys.argv) > 1):
        client = Client(sys.argv[1])
else:
        server = Server()
        server.run()

Everytime I run the code on the client end I keep getting the following error:

  File "chat.py", line 29
    print(str(a[0]) + ':' + str(a[1]), "connected")

The python code is exactly the same on the server and client end, but I don't get any issues on the server end.

 

Any Questions?

 

Thank you

Link to comment
https://linustechtips.com/topic/857438-building-chat-server-with-python/
Share on other sites

Link to post
Share on other sites

I don't think you gave us the full error it's giving

also it's not running anything right now when I run it. You don't call any of the functions

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

9 hours ago, LtStaffel said:

I don't think you gave us the full error it's giving

also it's not running anything right now when I run it. You don't call any of the functions

its at the bottom you need to add arga to your command line

 

python sometime.py host or if you don't but anything it should start the server. 

 

but yes we need the error stack not just the line it failed on. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×