Python Forum

Full Version: Question concerning function of a a socket
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My reverse engineering of torchat has led me to a question which I am unable to answer. When I look at the torchat source code, there is a file named tc_client.py, which contains the class “Listener“.

class Listener(threading.Thread):
    def __init__(self, buddy_list, socket=None):
        threading.Thread.__init__(self)
        self.buddy_list = buddy_list
        self.conns = []
        self.socket = socket
        self.start()
        self.startTimer()

    def run(self):
        self.running = True
        if not self.socket:
            interface = config.get("client", "listen_interface")
            port = config.getint("client", "listen_port")
            self.socket = tryBindPort(interface, port)
        self.socket.listen(5)
        while self.running:
            try:
                conn, address = self.socket.accept()
                self.conns.append(InConnection(conn, self.buddy_list))
                print "(2) new incoming connection"
                print "(2) have now %i incoming connections" % len(self.conns)
            except:
                print "socket listener error!"
                tb()
                self.running = False


    . . .
On line 19 of the code above, there is a socket on which the accept-method is called. As far as I know, the accept method can only be called on a server socket (I come from the JAVA world, where there exists a difference between a Socket and a ServerSocket object). Is the mentioned socket a server socket?
I only recently started to delve into sockets, but here is the socket module documentation. As you can see there is only Socket object and no distinct ServerSocket object. Socket object is used in server and client. This Listener class looks like threaded server listening for client connections.

here are two more useful links for reference
Socket Programming HOWTO
Socket Programming in Python (Guide)

EDIT: There is separate socketserver module which simplifies the task of writing network servers.
I don't know much about sockets either, but from what I have learned this code looks more like code for a server than a client. Perhaps why the script is named tc_client is because this file contains the part of the server that handles functions for the client like connections. But yes, this looks more like server code.
(Jan-17-2019, 06:25 PM)pkm Wrote: [ -> ]As far as I know, the accept method can only be called on a server socket (I come from the JAVA world, where there exists a difference between a Socket and a ServerSocket object). Is the mentioned socket a server socket?

Yes, in Python, a "socket" can be on both ends of the connection. There's no special distinction between listener/connector or server/client, they're just two ends of a pipe. That said, any time you're accepting connections on a socket, you can pretty much guarantee it's a server of some sort (or a peer to peer network, like for gaming, or Tor lol).
The Python sockets module is a tin wrapper around C Socktes. It's a little bit abstracted.
A server and a client use the same method to create a socket (Adress family, Protocol, Flags)
After the socket creation you decide to bind, if you want to be a server or connect if you're
a tcp-client. If you're using udp, then use sendto as client. UDP is connections less.

If you use C, all this methods are functions, which returns a file-descriptor or error code.