[ Part 1 (Overview)Part 2Part 3Part 4Part 5Part 6 – Part 7 ]

Welcome to my blog’s first tutorial series!

Make sure to watch for in depth code analysis.

In the first part of this project, the overview, we looked at the finished program, how it’s going to look once we wrap up the last part along with a lot of details about everything we’ll be covering along the way.

If you haven’t checked out the first part, go ahead right now, I’ll wait…

Also make sure you have all the required software: Python 2.7.

Done? Alright, grab your beverage of choice and get ready!

Setting Up Our Script

From this point forward, all the information is in the video guide, assuming you have already installed a text editor (like Sublime Text or Notepad++)…

Let’s begin by creating a nice folder to save our script. I decided to name mine PyCS.py (as in Python Chat Server, abbreviated accordingly).

Since we’re going to develop two scripts separately at first, I recommend having that folder so you don’t make things messy in your desktop.

It’s also useful in case you need to backup one of the scripts along the way.

Let’s Write Some Code

I will not go fully in depth on each line of code here in the blog, since I made a 30 minute video where everything is explained clearly.

In any case, I will lay down explanations for individual blocks of code here.

Also if you don’t care about writing the code yourself, I will leave the script for download at the bottom of each post for easy access. 😉

Enough talk for now, let’s look at some code!


# Librarys import
import os
import socket
import time, datetime

# Server configuration
USER = "Guest"
HOST = ""
PORT = 42020
CLTS = 1

# Client configuration
active = False		# Controls an active chat window
socks = []			# List of sockets
clients = []		# List of clients
interval = 1		# Interval to sleep (1sec = default)

As usual we begin by importing some librarys that we’ll need, then we jump right into server and client configuration variables. Keep in mind many of these won’t be used until we reach other parts of the guide (like active and the sockets/clients lists). I do like thinking about them right away though, as it hints you into what you need to work on next time. I’m also a big fan of leaving comments specifically on parts of code we are yet to work on (just in case we forget why we put them there in the first place).

# Clear function (keepin' things clean ;)
clf = 'clear'
if os.name == 'posix': clf = 'clear'
if os.name == 'nt': clf = 'cls'
clear = lambda: os.system(clf)

# Get time function
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%H:%M')
def GetTime():
	ts = time.time()
	st = datetime.datetime.fromtimestamp(ts).strftime('%H:%M')
	return st

Here we have some basic functions to clear the terminal screen and also fetch the time and properly format it to show hours and minutes.

# Send data function
def Send(sock, data, end="EOFX"):
	sock.sendall(data + end)

# Receive data function
def Receive(sock, end="EOFX"):
	data = ""
	l = sock.recv(1024)
		data = l
		if data.endswith(end) == True:
			l = sock.recv(1024)
	return data[:-len(end)]

Now we get into some socket functions to send and receive data. As mentioned before, I explain more about this in the video so I won’t break down each line here as it would take forever. So if you have any questions, feel free to ask in the comment section below.

# Username prompt and print it
	usr = raw_input("\n[*] Enter username: ")
	if len(usr) < 1:
		USER = "Guest"
		USER = usr
	USER = "Guest"

# Let's greet the user!
print "\n[" + GetTime() + "] Welcome " + USER + "!"

# Initialize the server socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
c.bind((HOST, PORT))
c.listen(CLTS)			# Listen for total of X clients

print "\n[" + GetTime() + "] Listening on " + HOST + " (" + str(PORT) + ") for a total of " + str(CLTS) + " clients."

Here we start executing code that interacts with the user. First we ask for the desired username, then we initialize what will become the main server socket. As usual, I will be outputting pretty much everything the script is doing on the screen (and later on save it in a system log) so that whenever we need to troubleshoot anything our job is easier.

At this point, we split our script into two: one for the server and another for the client. So go ahead copy and paste the script inside the folder. I renamed my duplicate PyCSc.py so I know it is the client script.

Now we’ll look to make slow progress on each script as to not jump any steps and make everything easily digestible (and easy to fix if we have to).

On the server script, we’ll continue like so:

# Server testing begins here ------------------------->


s,a = c.accept()
if (s):
	print "\n[" + GetTime() + "] Connection from " + str(a)

	while True:
		msg = Receive(s)
		if len(str(msg)): print "\n[" + GetTime() + "] " + msg

Notice the comment line indicating we are now “experimenting”…

Basically we will test our server and socket functions, so on the server side let’s just wait for a connection; once we receive one, let’s print it on screen and receive/output any messages we receive.

On the client script, we want to connect to it and send a message:

# Client testing begins here ------------------------->


CLIENT = raw_input("\n[" + GetTime() + "] What address are you connecting to? ")
PORT = raw_input("\n[" + GetTime() + "] What port are you connecting to? ")

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((CLIENT, int(PORT)))


Send(s, "Hello, this is the first message!")


Send(s, "Time for message number two! Ayyy ;)")

We’ll go ahead and ask the user what IP address and port to connect to (this should be another/or the same system in your network).

Then let’s send two messages to test the send function; the reason we run it twice is because if there is an error to run into, that’s our goal, we want to run into it now so we can fix it right away.

Download Code

If you feel like you’ve made some mistakes while following the guide or simply want to test the code for yourself, click here to download it!

So that’s it for the first part, we’ve covered a lot of ground! The upcoming parts will have less code, however, it will get more complex in nature – this being the reason why we break it down into multiple parts.

I’m clocking out, cya guys next time ayyyy!