Tuesday, 21 June 2016

Python Based Chat Program

This is a simple client-server Program to chat with other users on the Lan. This program largely similar to a chat using nc, easier to use and with a lot of less functionality.

Tested On:
OS: Fedora 20, Ubuntu 14.04 LTS
Language: Python

 Note

While this script works perfectly fine on loop back ie on the same machine, it may fail on a network since you might need to configure the server to accept incoming connections. You might want to check iptables or disable it completely. 

Video


Mediafire Download Link: Click Here 
Watch Without Compression High Quality Video
Download Size 1.5 MB

Code


#Script by Abhishek Munagekar for Programing Wonders
#For more visit www.prgwonders.blogspot.in
#Mode:
#1=Server (This must run before)
#2=Client (This must run after Server)
'''
WARNING
:The Author of the Program doesn't guarantee the security of this connection.
:Anyone Sniffing Packets on the Local Network Should Be Able to Obtain Entire Chat Conversation

POSSIBLE ERROR:
:Port Already Bound (use netstat -an|grep <portnumber>) before using a port number on server side
:Type only when you see Me: propmt, 

LIMITATIONS:
:This program largely acts like a Half-Duplex Connection
:No ability to check wether the port is already bound or not

HOW TO RUN:
:chmod +x cs.py(the name of the file)
:python cs.py(the name of the file)
:Enjoy
'''

import sys
import socket
if len(sys.argv) !=2:
 print "Usage: ",sys.argv[0],"Mode"
 sys.exit()

if (sys.argv[1])=='1':     #Server Code
 sname=raw_input("Enter your name: ")+":"
 s = socket.socket()         
 host = socket.gethostname() 
 port = int(input("Enter Port Number: "))             
 s.bind((host, port))       

 s.listen(5)                
 c, addr = s.accept()     
 print 'Got connection from', addr
 name=c.recv(32)+":"
 c.send(sname)
 while True:
      print c.recv(1024)
      print "Me:",    
      c.send(sname+raw_input())
 c.close()     

else:       #Client Code            
 cname=raw_input("Please Enter your Name:")+':'

 s = socket.socket()        
 host = raw_input("Enter Host ip Address:")
 port = int(input("Enter Port Number:"))              

 s.connect((host, port))
 print "Connection successful:"
 s.send(cname)
 name=s.recv(32)+":"

 while True:
  print "Me:",
  s.send(cname+raw_input())
  print s.recv(1024)
 s.close
 
 

Troubleshooting

  • Ensure that client can connect to the server by pinging to it.
  • Ensure that port used by server is not bound. Use netstat command.
  • In case of error on client side, ensure that server is already running.
In case you are facing connection issues to the server you might have to configure the iptable rules or disable the firewall.The following command might help.
systemctl stop firewalld

Also try out telenet command to see if the server is accepting connections. If telnet fails the script is unlikely to work.
telnet <serverip> <portno> .

No comments:

Post a Comment