Thursday, 1 October 2015

My first GitHub Project: A simple chat script in Python

This post is about a simple project that i uploaded on github. It allows you to chat between multiple computers on the same network.

There is a library named "socket" in Python, that allows you to open ports, listen for requests on these ports and send & receive data between multiple machines using this. A "socket" is basically an end point in a Network. I wrote a couple of scripts that allow you to simply share text messages between multiple machines on the same LAN Network. A '\q' message means quit.

properServer.py:
#!/usr/bin/python

import socket
import sys
import os

def listen1():
while True:
try:
message1 = connection1.recv(180)

if message1 == '\q':
connection2.send(message1)
connection1.shutdown(socket.SHUT_RD | socket.SHUT_WR)
connection2.shutdown(socket.SHUT_RD | socket.SHUT_WR)
connection1.close()
connection2.close()
server.close()
print "Connections closed"
break


if message1:
connection2.send(message1)
print "message",message1,'sent from client1 to client2'

except:
try:
connection1.shutdown(socket.SHUT_RD | socket.SHUT_WR)
connection2.shutdown(socket.SHUT_RD | socket.SHUT_WR)
connection1.close()
connection2.close()
server.close()
print "An error occured. Connections closed"
break
except:
pass


def listen2():
while True:
try:
message2 = connection2.recv(180)
if message2 == '\q':
connection1.send(message2)
connection1.shutdown(socket.SHUT_RD | socket.SHUT_WR)
connection2.shutdown(socket.SHUT_RD | socket.SHUT_WR)
connection1.close()
connection2.close()
server.close()
print "Connections closed"
break

if message2 != ' ':
connection1.send(message2)
print "message",message2,'sent from client2 to client1'

except:
try:
connection1.shutdown(socket.SHUT_RD | socket.SHUT_WR)
connection2.shutdown(socket.SHUT_RD | socket.SHUT_WR)
connection1.close()
connection2.close()
server.close()
print "An error occured. Connections closed"
break
except:
pass



server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)


server_address = ('192.168.1.6',8081)
server.bind(server_address)
print "Server started on port 8081"

server.listen(2) #listens for connection request from clients
connection1,client_address1 = server.accept() #accepts a request from client
print 'client1:',connection1.getpeername()

server.listen(2) #listens for connection request from clients
connection2,client_address2 = server.accept() #accepts a request from client
print 'client2:',connection2.getpeername()

newpid = os.fork()

if newpid == 0: #child process
listen1()
parentId = os.getppid()
os.kill(parentId,signal.SIGKILL)


else:
listen2()



What this does is, it sets up one of the machines as server, that listens for requests. If a client tries to connect to the machine on that IP Address, it accepts the upcoming request. It then receives a message from one client and sends it to the other. Note that only two clients can be connected at a time. Pretty straightforward. This script can be run on one of the machines on which the client script is running, or a third machine may be used that can be used as a server.

client1.py:
#!/usr/bin/python

import socket
import os
import sys
import signal

client1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client1.connect(('192.168.1.6',8081)) #ip address of machine where server is running

def inp():
while True:
try:
message = raw_input('Me: ')
if message:
client1.send(message)
if message == '\q':
print ""
break

except:
break

def outp():
while True:
data = client1.recv(180)
if data:
if data == '\q':
print ""
break
print '\nReceived: ',data


newpid = os.fork()

if newpid == 0: #child process
outp()
parentId = os.getppid()
os.kill(parentId,signal.SIGKILL) #kill parent, then exit

else:
inp()
parentId = os.getpid() #pid returns own, ppid returns parent's process id
os.kill(parentId,signal.SIGKILL)


This script sends a connection request to the server and then a simple prompt is displayed, that allows user to type messages. A message is sent when the user presses enter. If a message is received, it is displayed on the same screen. Below are some screenshots of the above setup:

 

System 1(Where properServer.py and one instance of client1.py is running):

Chat Python Chat Python

System 2 (Where another instance of client1.py is running)

Chat Python Chat Python

 

You can check out these scripts on my github repository: https://github.com/nikssardana/ChatPython

Any comments, suggestions, improvements are always welcome :)

No comments:

Post a Comment