1: Simple TCP Python Server

Hi everyone!  In this tutorial I will be showing you how to create a simple TCP (socket) server in the python programming language.

Lets begin:

In this tutorial, we will be using the twisted module.  If you are using a mac (like me) this should already be installed.  If you are on Windows, you may need to download it here: Twisted Web

Now, create a new document in your editor of choice (I like TextWrangler for Python) and name it Server.py

We need to import twisted, so at the top of the file put:

from twisted.internet.protocol import factory, Protocol
from twisted.internet import reactor

Now that we have everything, lets get the server up and running. Add this after the imports:

fact = Factory()
reactor.listenTCP(1234, fact)
reactor.run()

The first line creates a new variable called fact which is an instance of Factory()

The second line is where you specify what port you want your server to be “listening” on. In this case, the port is 1234.

Finally, we tell the reactor to run. (Starts the server!)

You now have a fully functional TCP server! (Albeit you can’t really do anything with it)

Run it with the Terminal command:  python Server.py

Now that we have that, I’m going to show you how to actually do something with your server.

In between the import statements and the rest of the code, write the following:

class MyServer(Protocol):
     def connectionMade(self):
          print 'A client done diddely connected!'

This creates a subclass ,if you will, of Protocol, and  connectionMade(self): will be called every time a client connects.

Add a print statement before reactor.run() saying “Server started”, or something similar.

So far, your entire code should look like this:

from twisted.internet.protocol import factory, Protocol
from twisted.internet import reactor

class MyServer(Protocol):
     def connectionMade(self):
          print 'A client done diddely connected!'

fact = Factory()
reactor.listenTCP(1234, fact)
print 'Server started, listening for clients'
reactor.run()

To test this out, run it the same way I told you earlier.  Open another terminal window and in it type:

telnet localhost 1234

If all goes well, your server window should display your client connected message!

Congratulations, you have created a very simple socket server in Python!

Please leave any comments or questions below, make sure to follow me on twitter for updates on more tutorials and tips!

Till’ then,

Tate [AllTheWay Apps]

**Read this for more defs that can be used to create a more powerful and useful server: Writing Servers

Advertisements

About allthewayapps
We are a company aimed at making quality, entertaining games that you will enjoy for a long time. Check back frequently for regular updates!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: