Python Sockets

Last time I talked about a project I was working on and some python CGI research. Following up on that, that project also involves a little socket programming (which I chose to do in python again). Here is some wikipiedia info on internet sockets. We are going to be using streaming sockets with TCP (Transmission Control Protocol). Basically, a socket allows you to tunnel bytes (usually characters) from one computer to another (or many). Generally with TCP, you have a socket server that waits for socket clients to connect. Then it spawns off threads and serves that client socket resources. Usually, in the case of most sockets on the internet, those resources are html pages, pictures, etc. Sockets are very low level and in most cases considered a very complex programming subject. But, for simple projects, like the one I am working on, they are actually quite easy to implement. Here is the package for this post.

First take a look at threat_server.py, first we are importing everything from the socket module into the namespace, we are also importing urllib2, that is used to parse the Department of Homeland Security site and get the current threat level. More on that later. Next we establish some constants:

    HOST = ''
    PORT = 9055
    BUFSIZ = 1024
    ADDR = (HOST, PORT)

HOST is null b/c this is the server, this basically means localhost. PORT can be arbitrarily chosen so long as it is not being used by some other program. Don’t choose one under 100! I would just stick to numbers over 3000. BUFSIZ is naturally the size of the receive buffer. ADDR is a tuple containing HOST and PORT.
Now we have this chunk of code:

    tcpSerSock = socket(AF_INET, SOCK_STREAM)
    tcpSerSock.bind(ADDR)
    tcpSerSock.listen(5)

The first line establishes the variable tcpSerSock (our socket), and creates it as a tcp socket stream. AF_INET and SOCK_STREAM are predefined in the namespace when we import the socket module. This is the default configuration. Next we “bind” this socket to our address. Then the listen() function tells the socket the max amount of connections to listen for. This could just as easily be 1 for this example.
Next, we enter an infinite “while True” loop. and listen for a client socket:

     tcpCliSock, addr = tcpSerSock.accept()

After we get one, we enter another infinite loop where we wait for data from the client socket:

     data = tcpCliSock.recv(BUFSIZ)

We break the loop and listen for another client if this client sends “nothing” or the string “disconnect”. If the client sends “getTime”, we send the time. If they send “getThreat”, we send the current threat level. So on and so on. So, even though this isn’t a very powerful example, you can see the possibilities. Also, this code is not stable at all. There should be timeouts, multiple threads, etc., but that would be for a production quality piece of code and this is just an example.

Next open up the client, clienttest.py. This one is simple. You see some of the same setup as the server except HOST is foreign. If you are using this on your sever, change this to your domain. Also, make sure the port is chosen the same as your server program. The rest of this program is self-explanatory.
So, how to use this stuff. You can test this over your localhost or over a server. If you do the former, change the HOST in both programs to ‘localhost’ or maybe ‘127.0.0.1′. Then run the server, then run the client in a different shell. If you do the latter, FTP the server program to your server and run it there. Then run the client on your machine.
Well, this is all pretty simple but this is going somewhere. I will release more details later in the week when school dies down.