Building a Python server

Now, we're going to set up a simple server. I say simple server, which may make you think something like an HTTP server with just basic functionality—no, I mean simple. This will simply listen for connections and take an action upon receipt of data. Let's take a look:

#!/usr/bin/pythonimport socketimport threadinghost_ip = '0.0.0.0'host_port = 45678server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((host_ip, host_port))server.listen(4)print "Server is up. Listening on %s:%d" % (host_ip, host_port)def connect(client_socket):    received = client_socket.recv(1024)    print "Received from remote client:\n-----------\n%s\n-----------\n" % received    client_socket.send("Always listening, comrade!\n\r") print ...

Get Hands-On Penetration Testing on Windows now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.