Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Python server
    text
    copied!<p>How can I start a simple python server that will allow me to connect to sockets from some outer source ?</p> <p>I've tried :</p> <pre><code>import SocketServer class MyUDPHandler(SocketServer.BaseRequestHandler): def handle(self): data = self.request[0].strip() socket = self.request[1] print "%s wrote:" % self.client_address[0] print data socket.sendto(data.upper(), self.client_address) if __name__ == "__main__": HOST, PORT = "localhost", 80 try: server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler) print("works") server.serve_forever() serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind((socket.gethostname(), 80)) serversocket.listen(5) except: print("nope") while True: (clientsocket, address) = serversocket.accept() ct = client_thread(clientsocket) ct.run() </code></pre> <p>But when I'm sending something to the server I don't get any info. How can I change this code to see if someone is sending some data ?</p> <hr> <p>EDIT</p> <p>Now I've found this code :</p> <pre><code>class mysocket: """demonstration class only - coded for clarity, not efficiency """ def __init__(self, sock=None): if sock is None: self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM) else: self.sock = sock def connect(self, host, port): self.sock.connect((host, port)) def mysend(self, msg): totalsent = 0 while totalsent &lt; MSGLEN: sent = self.sock.send(msg[totalsent:]) if sent == 0: raise RuntimeError("socket connection broken") totalsent = totalsent + sent def myreceive(self): msg = '' while len(msg) &lt; MSGLEN: chunk = self.sock.recv(MSGLEN-len(msg)) if chunk == '': raise RuntimeError("socket connection broken") msg = msg + chunk return msg </code></pre> <p>but how to use this stuff to just listen to sockets and receive data sent ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload