Python Programming Socket Server and Client Chatting

Chatting Python Socket Program Based on IPV4

This program is used to establish socket connection and communicates the messages between server and client based on IPV4 protocol.

Server Program 'server.py'

# Chatting server program                                                       
import socket                                                                   
                                                                                
# server host address, denotes localhost if empty string.                       
HOST = '';                                                                      
# port which is used to communicate server and client programs                  
PORT = 40005;                                                                   
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sc:                   
    sc.bind((HOST, PORT));                                                      
    sc.listen(1);                                                               
    connection, address = sc.accept()                                           
    with connection:                                                            
        print('Connected by client host: %s' % repr(address))                   
        while True:                                                             
            data = connection.recv(1024)                                        
            if not data:                                                        
                break;                                                          
            # printing client message here.                                     
            print("Received from client: %s" % repr(data));                     
            data_to_send = input("Enter message to client:");                   
            if not data_to_send:                                                
                break;                                                          
            # sending message as bytes to client.                               
            connection.sendall(bytearray(data_to_send, 'utf-8'));

Client Program 'client.py'

# Chatting client program                                                       
import socket                                                                   
                                                                                
# host address where client program is running.                                 
HOST = '';                                                                      
# port on which server and client programs are communicating.                   
PORT = 40005;                                                                   
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sc:                   
    sc.connect((HOST, PORT))                                                    
    while True:                                                                 
        data_to_send = input("Enter message to server: ");                      
        if not data_to_send:                                                    
            break;                                                              
        # sending message as bytes to server.                                   
        sc.sendall(bytearray(data_to_send, 'utf-8'));                           
        data = sc.recv(1024);                                                   
        # printing server message.                                              
        print('Received from server: %s'% repr(data));  
Server Output:
$ python3 server.py 
Connected by client host: ('127.0.0.1', 43944)
Received from client: b'hi'
Enter message to client:hi, how are you?
Received from client: b'Good, where are you?'
Enter message to client:
Client Output:
$ python3 client.py 
Enter message to server: hi
Received from server: b'hi, how are you?'
Enter message to server: Good, where are you?

Chatting Python Socket Program Based on IPV6

This program is used to establish socket connection and communicates the messages between server and client based on IPV6 protocol.

Server Program 'server.py'

# Chatting server program                                                       
import socket                                                                   
import sys                                                                      
                                                                                
# host address where server program is running. here None denotes localhost.    
HOST = None;                                                                    
# port which is used to communicate server and client programs                  
PORT = 40005;                                                                   
sc = None                                                                       
for address_info in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,            
                                       socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
    af, socket_type, protocol, canon_name, sa = address_info;                   
    try:                                                                        
        sc = socket.socket(af, socket_type, protocol);                          
    except OSError as msg:                                                      
        sc = None;                                                              
        continue;                                                               
    try:                                                                        
        sc.bind(sa);                                                            
        sc.listen(1);                                                           
    except OSError as msg:                                                      
        sc.close();                                                             
        sc = None;                                                              
        continue;                                                               
    break;                                                                      
if sc is None:                                                                  
    print('Connection is not established between server and client.');          
                                                                                
connection, address = sc.accept();                                              
with connection:                                                                
    print('Connected by client host: %s' % repr(address));                      
    while True:                                                                 
         data = connection.recv(1024);                                          
         if not data:                                                           
             break;                                                             
         print("Received from client: %s" % repr(data));                        
         data_to_send = input("Enter message to client:");                      
         if not data_to_send:                                                   
             break;                                                             
         connection.sendall(bytearray(data_to_send, 'utf-8'));   

Client Program 'client.py'

# Chatting client program                                                       
import socket                                                                   
import sys                                                                      
                                                                                
# host address where client program is running.                                 
HOST = 'localhost';                                                             
# port which is used to communicate server and client programs                  
PORT = 40005;                                                                   
sc = None;                                                                      
for address_info in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
    af, socket_type, protocol, canon_name, sa = address_info;                   
    try:                                                                        
        sc = socket.socket(af, socket_type, protocol);                          
    except OSError as msg:                                                      
        sc = None;                                                              
        continue;                                                               
    try:                                                                        
        sc.connect(sa);                                                         
    except OSError as msg:                                                      
        sc.close();                                                             
        sc = None;                                                              
        continue;                                                               
    break;                                                                      
if sc is None:                                                                  
    print('Connection is not established between server and client.')           
with sc:                                                                        
    while True:                                                                 
        data_to_send = input("Enter message to server: ");                      
        if not data_to_send:                                                    
            break;                                                              
        sc.sendall(bytearray(data_to_send, 'utf-8'));                           
        data = sc.recv(1024);                                                   
        print('Received from server: %s'% repr(data));   
Server Output:
$ python3 server.py 
Connected by client host: ('127.0.0.1', 60248)
Received from client: b'hi'
Enter message to client: hi, how are you?               
Received from client: b'chatting in python program'
Enter message to client:
Client Output:
$ python3 client.py 
Enter message to server: hi
Received from server: b' hi, how are you?'
Enter message to server: chatting in python program
Received from server: b''

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^