Friday, April 6, 2018

Simple Server and Client Chat using Python

Server-Client Chat Python

In Python language, socket (or network socket) is a module used to communicate between two computers. It provides two types of interface to access the network, namely low-level (platform dependent connections — Example: Telnet) and high-level (application dependent connections — Example: HTTP, FTP, SMTP, etc.). This is a simple tutorial to establish the low-level socket connection between server and client to communicate messages using the TCP/IP protocol.

Server and Client Chat

In this tutorial, I have used two scripts server.py to serve data by sending an invitation to the client, and client.py to receive data on acceptance of the invitation. After accepting the invitation, both server and client share messages mutually.

— Server —

An server script performs the sequence of functions such as socket(), bind(), listen(), and accept() (repeats for more than one client) to communicate with the client. The description of each functions used in the server script are given bellow:

  • socket() – creates a socket using the address family, socket type and protocol
  • bind() – binds the socket to the given address (host name, and port number pair)
  • listen() – enables a server to accept connections from the client(s)
  • accept() – waits and accepts connection request from the client(s)
  • gethostname() – retrieves host name of the machine
  • gethostbyname() – translates a host name to IPv4 format address
  • recv() – receives message sent through TCP
  • decode() – decodes the message using the codec
  • send() – sends message sent through TCP
For more details on methods, functions, module, and objects, refer to the official site of Python (python.org).

Source Code

# server.py
import time, socket, sys

print("\nWelcome to Chat Room\n")
print("Initialising....\n")
time.sleep(1)

s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")\n")
name = input(str("Enter your name: "))
           
s.listen(1)
print("\nWaiting for incoming connections...\n")
conn, addr = s.accept()
print("Received connection from ", addr[0], "(", addr[1], ")\n")

s_name = conn.recv(1024)
s_name = s_name.decode()
print(s_name, "has connected to the chat room\nEnter [e] to exit chat room\n")
conn.send(name.encode())

while True:
    message = input(str("Me : "))
    if message == "[e]":
        message = "Left chat room!"
        conn.send(message.encode())
        print("\n")
        break
    conn.send(message.encode())
    message = conn.recv(1024)
    message = message.decode()
    print(s_name, ":", message)

— Client —

An client script performs the sequence of functions such as socket(), and connect() to communicate with the server. The description of each functions used in the server script are given bellow:

  • socket() – creates a socket using the address family, socket type and protocol
  • connect() – connects to a server socket at address
For more details on methods, functions, module, and objects, refer to the official site of Python (python.org).

Source Code

# client.py
import time, socket, sys

print("\nWelcome to Chat Room\n")
print("Initialising....\n")
time.sleep(1)

s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")\n")
name = input(str("Enter your name: "))
           
s.listen(1)
print("\nWaiting for incoming connections...\n")
conn, addr = s.accept()
print("Received connection from ", addr[0], "(", addr[1], ")\n")

s_name = conn.recv(1024)
s_name = s_name.decode()
print(s_name, "has connected to the chat room\nEnter [e] to exit chat room\n")
conn.send(name.encode())

while True:
    message = input(str("Me : "))
    if message == "[e]":
        message = "Left chat room!"
        conn.send(message.encode())
        print("\n")
        break
    conn.send(message.encode())
    message = conn.recv(1024)
    message = message.decode()
    print(s_name, ":", message)

Implementation Steps

1. Run the server.py script in Python application.

Python Open With

2. Note down the local IP address and pass it to the clients (invitation). Alternatively, you can also get the local IP address through online using L-IP tool.

Python Server Chat

3. Run the client.py script in Python application using the local IP address sent by the server (accept the invitation).

Python Client Chat

4. Sent and/or receive messages from the server/client mutually.

Python Server Chat Messages

Location: Kulinjanvilla, Alencode Road, Nambali, Tamil Nadu 629154, India

0 comments :

Post a Comment