12
Sep
Python Socket Programming
A little while ago I blogged (still can't believe that's considered a word) about a simple MSN client I tried to write in PHP (you could read about it here). It didn't really work out in the end, because I needed threads, but this -once again- got me interested in multi-threaded socket programming (this is something you could say at a dinner party).

Using sockets you can easily set up and manage network connections, so that you can write sophisticated clients, servers and agents. The multi-threaded aspect entails that you can manage multiple connections at the semi-same time; this is the reason my MSN client project got stuck, because PHP is still completely single-threaded and the MSN protocol requires multiple connections to conversations (so called 'chatboards') to be satisfied.

As you could've read in my blog about the MSN Bot, I started thinking about using an other language in which to rewrite the client. Well, I didn't come to the last part yet, but I did find a new language that serves all my needs (for now, at least): Python!

I have had some minor experience with Python a couple of years ago, but at that time I couldn't really get in to it (mainly because of my English reading skills and my lack of knowledge of programming-jargon at that time. You will find every coder uses a lot of 'computery words' and for that I'd like to apologise, because it sets the bar for a novice pretty high.) Just recently I contemplated about using it again and before I knew it I had written a simple web server in Python! It runs and serves a lot faster than Apache does by the way, but that's probably because it's a very small server with little to no features (and full of bugs for that matter). It works great for my little LAN network at home though, but once again I left out the 'multiple connections' bit, because I was just trying to get some experience in Python and needed a little more than the famous 'Hello World' to get started...but now I was ready!

It must be about a year ago when I used threads for the last time, because I've done a lot of internet programming in the past year and never ran into a problem that might have needed threads. In Python it was really easy to do though and I had a simple example running in no time. Instead of just finishing my web server I started on a new application to test out the multi-threaded and socket abilities of Python: a public chat server.

Once again: the fundament of the program could be written really fast and I had something pretty descent going within a couple of hours. Using telnet from the command-line to connect you could start typing and sending messages to everybody else that was connected and it worked fine. I will definitely be running that at my next LAN party (whenever that may be). I might release the code soon, but it isn't really ready right now (it being not documented and ill designed) so you'll have to wait for that if you're even slightly interested :P

To conclude: working with Python and it's sockets and threads was really simple, fast and intuitive and I will use it a lot more in the future. I also like to mention doxygen which is a fine document generator for C, C++, Java and Python.

That's it for now. This is probably my longest 'blog' ever and it doesn't even contain a youtube video! How about that! I'll probably do some more technical, coding blogs because it's a little more worthwhile than just saying 'Check out this amazing video!!', although I like that too :P See ya!



2 Comments


1
So it's NOT a tutorial?
Written by: raaandoms site
2009-10-12 09:27:32
And you got me all worked up...


2
RE: Python Socket Programming
Written by: Baby Python
2010-03-09 03:20:28
Uhk, uhk, uhk, tot tis is a tutorial too... so sad it's not!


Leave a comment
Name*
E-mail
Website
Title*
Comment*
Notify me when somebody else comments on this article