What does it take to implement a chat system in Python (Not asking for code just advice before I start my little project)

Chris Angelico rosuav at gmail.com
Thu Jul 18 08:15:29 CEST 2013

On Thu, Jul 18, 2013 at 2:36 PM, Aseem Bansal <asmbansal2 at gmail.com> wrote:
> I wanted to do a little project for learning Python. I thought a chat system will be good as it isn't something that I have ever done.

A good thing to start with. Yes, it's been done before, many times...
but if you think about it, it's the fundamental on which pretty much
everything else is derived. The step from a multi-person chat system
to a multiplayer game is very slight.

> I wanted to know what will I need? I think that would require me these
> 1 learn network/socket programming
> 2 find a free server to host my chat server
> 3 GUI development for clients

Learn network programming, definitely. As an adjunct to that, learn
networking security. For instance, do not think in terms of "the
client will only ever send X"; the server has to be able to cope,
safely, with any stream of bytes coming from the socket. Also, be sure
you understand the difference between bytes (or octets) and Unicode
characters, and know what everything is. Python 3 helps with this.

Finding hosting (I'll use the word "server" here to mean the program,
and will say "hosting" when I mean a computer) is optional though; for
your first tests, use your own computer. You can run the server and a
number of clients all on the same computer, and alt-tab between them;
or, if you have a LAN, you can use that. Then when you want to expand
to the full internet and let your friends in on this, you can still
host it on your home internet connection, though you may need to
choose carefully which port you use (some home connections prevent
incoming port 25 and 80 traffic). You won't need to worry about
hosting until (a) you need it to be up 24x7 and don't want to depend
on your home system, and/or (b) you need more bandwidth/processing
than your home connection will do. Neither will be true of your first

GUI development, in my opinion, should be left for Phase Two. Start by
making a very simple system that just works with plain text; later on,
make a fancy graphical interface. Keep the text version working, and
keep them compatible. Trust me, you'll appreciate that text one when
you start debugging - it'll be so easy to see what's going on.

> -I wanted to know whether these are all that I would need or are there more things?
> -Will I need to learn a web framework like Django?

Hmm. I would think not; I'd recommend that a chat system work with TCP
sockets directly, for simplicity and performance. Working with a web
framework implies working with HTTP, and unless you're running this in
a web browser, there's no reason to do that.

> -Will I need to learn something for database management like sql for handling people's account names and password?

Optional. Leave that for Phase Two; to start off with, just let people
type in their own names, and don't worry about authentication
initially (this isn't such a crazy idea - IRC largely works this way).
You can add authentication later.

> Any other advice for me(a novice programmer)?

Get to know as many tools as you can, so that when you're faced with a
problem, you can select the right one for the job. You are not Jeremy
Clarkson, your toolchest is not all hammers :)

In this particular instance, you may find that Python is the best
language for the clients, but not for the server. I've written a
number of chat-server-like systems, most notably MUD clients and
derivatives, and I use Pike for the server. Its biggest advantage
(over Python) is that you can tweak the code while it's running; I've
had Pike servers running for over two years on commodity hardware (and
would still have that uptime today if the UPS hadn't died). Still
trying to claw all that back.. up to 25 weeks so far. Pike and Python
are extremely similar in semantics (even down to having a
nearly-identical internal string representation, as of Python 3.3),
but have distinctly different syntax (Pike looks like C), and their
optimizations are quite different, so performance varies somewhat.
They're both high level languages that let you manipulate functions,
dictionaries, etc, as first-class objects, they're both sufficiently
high performance/efficiency to run something like this on 0.00 load
average (my server's load is more usually from Apache serving PHP
pages than it is from either Pike or Python), and both will serve you
well in this project.

One more tip: Don't be afraid to ask for help! I personally *love*
networking, and will gladly help out with any little problems you run
into; the same, I am sure, will be true of a good number of people on
this list. Networking is a complicated world, and there are a lot of
odd concepts to master; but it's also an immensely fun bunch of
technologies. Why play with just one computer when you can play with
half a dozen!


More information about the Python-list mailing list