How to Make A Simple Python Chat Server

If you want to chat with your friend using command prompt, this article would be very helpful to you.We are not using any social networking services here,Even we don’t need any browsers either.

At the end of the article,I’m sure about that you will learn how to setup the Python client and server to chat with another person of same Local Area Network(LAN).

Assumptions:

  • Your Python version should be 2.7.x
  • You and Your friend should be on the same LAN
  • A Little knowledge about how  client and server architecture works.

Python Network Programming

I will give you the detailed introduction about every module/method that I am going to use here.Basically,Python has so many modules which dealt with their predefined methods for work to be done.One of the best  and mandatory modules for networking is “Socket Module”.Unless connection is established through sockets, we aren’t able to communicate with the other systems

Okay, let’s move forward!! 🙂

As I mentioned earlier,We have to create two files such as server.py and client.py

Also Read :   FreeBitco.in Lives Calculator

Python  simple Server socket Programming

Note : Click on the Images for bigger sizes

python-server

The first step is just to import socket modules and its methods.

the second step is  we are assigning the socket function to a variable “S” which is having two arguments.The first argument is used for IPV4 and the second one is for TCP (SOCK_STREAM) where as UDP is SOCK_DGRAM

The third  is s.bind(( )) function which is having two arguments for host and ports.we are assigning HOST  value as null..so the default value will be 0.0.0.0 and it is always waiting for client who has to connect on  port 9999

The fourth is listen function,as I said, the server is always waiting for new connections on a certain port, so we can permit the server to listen  on only a  few clients.

conn, addr = s.accept() defines that it should accept  the connection from client address (addr) whereas conn is server address.we have to use it for further requests.Now it is waiting for the connection from a client.So you are not able to write any command until the connection from the client is established.

Also Read :   FreeBitco.in Lives Calculator

That’s it with python socket server

Python Client Socket Programming

python-client-server

For sockets,Unlike server.py We don’t need to bind  instead we have to connect to the host.In my case, I would like to connect to the host where  the server.py is running i.e., 192.168.1.52 on the port number 9999.(So you should know your friend IP address to connect)

If the server accepts the connection, then only we get prompt to send further requests to the server.

python socket server

As soon as the connection is established, we can start chat between the server.py and client.py.

Basically,We have a method which is available in socket module called send()  to  send  the requests and we can receive the response from the server to the client system by recv() method simultaneously.We can easily understand it by above picture

I have sent 3 messages to 192.168.1.52 and it gives output as 21  which actually represents the total no of characters in that message but it is not the server response.For Server response, I am using s.recv(1024) to print the response of 1024 characters on the screen.

Also Read :   FreeBitco.in Lives Calculator

python-network-programming

From the above image, we can understand that server has received a message by using conn.recv() method.If you notice the above command you may have a query that why we are using conn instead “s”, because when the connection is established,”s” doesn’t rely on “0.0.0.0” to communicate with the client. To get some idea about conn and addr, type

print conn

print addr

At last, we have to close the sockets by s.close() method.

Note: we can’t send strings as messages to the server/client in Python 3.x.x versions.It only works on < 3 versions.This is a small and simple example of python client server socket example and I write this code as simple as possible to understand without using any methods in the code.

That’s simple 🙂

 

Leave a Reply

Your email address will not be published. Required fields are marked *