1

I'm somewhat of a n00b to Python and I'm working on a tiny project. This is my code in src/sock.py

import socket
import config
class Server(socket.socket):
    def __init__(self):
        socket.socket.__init__(self, socket.AF_INET, socket.SOCK_STREAM)

    def start(self):
        self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.bind((config.bind_host, config.bind_port))
        self.listen(5)

        while True:
            pass

and my code in start.py

import src

Socket = src.sock

Socket.Server()
Socket.Server.start

but the Server doesn't seem to be starting. :(

Any help would be much appreciated

1 Answer 1

1

Your code:

Socket.Server()

will create a server instance. But since you don't assign that created instance to a variable, you can't use it or reach it (and it will be garbage collected very quickly).

Socket.Server.start

accesses the start method on the Server class (not the created instance, but the class). But again, you don't do anything with it. You don't call it, you don't assign it to anything. So it is in effect a noop.

You need to assign the created server instance to a variable, and then call the start method on that instance. Like so:

server = Socket.Server()
server.start()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.