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

Welcome to my blog’s first tutorial series!

Watch the video for some code analysis!

Hey guys! Ready to write some code?

We’ve put a lot of work in these past five parts and today is no exception, however, we’ll try to take it easy after that insanity in the last article.

Looking back I probably should’ve broken that last article in more parts. At the same time, I guess it serves to separate the kittens from the lions. 😀

Enough jokes for now, let’s keep moving…

Today we’ll be implementing the second part to the text-based user interface which is what I call the active client chat screen.

Of course this also requires us to first implement the logging system for all of our clients, along with notifications as well.

Besides that we’ll add a few more features to wrap it up.

Dive Into The Code

I’m gonna focus on the main blocks of code added this time, so right off the bat, let’s jump into the new ‘active client chat screen’ interface…

# Main client chat screen
def RefreshChat(client):
	print ' - Python Chat Server\n'
	print '\nChatting with ' + str(client) + ' (' + str(len(clients)) + ' clients active)...\n'
	print '\n------------------------------\n'
	if len(clientlog) > 0:
		for j in clientlog:
			if str(j[0]) == str(client):
				if len(j[1]) > 0:
					for k in j[1]:
						print k
		print '...\n'
	print '\n'

This code actually looks more complex than it really is. Basically it display which client we are chatting with, then it goes into some loops.

We iterate through our list of client logs, compare if the username of the active client matches the one being iterated. If it does we’ll simply go ahead and display every message (both clients’ and ours) stored in it.

# Print text and record it in client logs
def printc(client, text):
	for i in clientlog:
		if i[0] == client: i[1].append(text)
	print text

If you remember from last time, we created a function very similar to this one in order to save all of the system logs into a list.

This time we’re gonna use it to add every message from that specific client into his own section inside of the client’s log list – and print it too.

# Add client to the clients list ('username', IP, Port)
					clients.append((msg[-(len(msg)-5):], str(a[0]), PTAV))
					clientlog.append([str(msg[-(len(msg)-5):]), []]) # Add client to logs
					notifs.append([str(msg[-(len(msg)-5):]), 0])	 # Add client to notifications

In the same area inside the main server thread we’re we add the client into our list, we’ve gone ahead and added them to the logs and notifications.

while True:
					active = True
					if notifs[act][1]: notifs[act][1] = 0 # Clear notifications
					ms = raw_input("\n[" + GetTime() + "] Enter message: ")
					if ms == 'quit' or ms == '':
						active = False
						Send(socks[act], ms)
						printc(str(clients[act][0]), "\n[" + GetTime() + "] <" + USER + "> " + ms)

Finally we’ve added some code to the main script execution in order to activate the ‘active client chat screen‘.

Notice how the variable active controls whether we have a client activated. Also, not only do we send the client our message, but now we have to go ahead and store our message in the logs as well.

Download Code

That’s it for this part guys, keeping it short and sweet.

We’re almost reaching the end of this little series, we have one part left.

In this last part I’ll be looking to make some functions to gracefully close the connections, along with correcting some possible errors. I do realize some errors are going to slide (would love some help to find the little issues), but creating a flawless program was never the intention.

Perhaps I can upload it to github or a different platform later on in case somebody decides to put some work to improve this little project.

Code On By! 😉