Simple Python server
Question: How can I start a simple python server that will allow me to connect to
sockets from some outer source ?

I've tried :

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

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 ?

* * *

EDIT

Now I've found this 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 < 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) < MSGLEN:
                chunk = self.sock.recv(MSGLEN-len(msg))
                if chunk == '':
                    raise RuntimeError("socket connection broken")
                msg = msg + chunk
            return msg
    

but how to use this stuff to just listen to sockets and receive data sent ?


Answer: The simplest solution is
[SocketServer](http://docs.python.org/library/socketserver.html), which is
part of the Python standard library.

