Python Forum
Can't get socket application to run
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't get socket application to run
#1
Hello,
I am trying to get this code to work. I am following a guide from YouTube to make a python Socket chat application, but get errors when I attempt to run. Attached are the outputs when running server and client sides of the application. Any help would be appreciated.

Outputs:
Output:
Traceback (most recent call last): File "C:\py\SocketChat.Py", line 58, in <module> client = Client(sys.argv[1]) NameError: name 'Client' is not defined


and

Output:
Traceback (most recent call last): File "C:\py\SocketChat.Py", line 61, in <module> server.run() TypeError: run() missing 1 required positional argument: 'self'
The 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(('127.0.0.1', 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:
                       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(self.connections)


        
        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(data)



if(len(sys.argv) > 1):
    
        client = Client(sys.argv[1])
else:
        server = Server
        server.run()
Reply
#2
Remember that in python the indentation is important. Your class Client is inside the class Server when it shall be at the same level of the class Server (translated: remove the extra indentation from line 34)

One thing strange is that you mix 8 spaces indentation with 4 spaces... maybe I am wrong but that is typical from using an editor with hard tabs. If you are working in windows I recommend to you to use notepad++ that will enforce you to use consistent tab/space usage. The recommended settings for python is indent with 4 spaces, no tabulators (\t char, with a nice editor you can press tab and it will insert 4 spaces for you)
Reply
#3
You have forgotten to instantiate an object from class Server.
Currently you assign the class Server to the name server.
server.run is accessing Server.run, which is a bound method.
Bound method means, you can use is only after the object has
been instantiated from class.

#wrong
        server = Server
        server.run()
#right
        server = Server()
        server.run()
Yeah, sometimes there is a forest, but we don't see the woods.


EDIT: Yes, this also my first thought. Maybe the indentation was outside of the class.
But I guess it's only the missing call of the class to create an object.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to send data from a python application to an external application aditya_rajiv 1 2,319 Jul-26-2021, 06:00 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020