0% found this document useful (0 votes)
72 views

Python Assignment

This document contains code for a client-server program using sockets in Python. It includes code for both the server and multiple clients. The server code establishes a socket and listens for incoming connections, while client code connects to the server socket and sends/receives data. There are 4 questions with variations in the code for different client-server interactions, such as the server receiving and returning messages in various formats.

Uploaded by

Imaad Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Python Assignment

This document contains code for a client-server program using sockets in Python. It includes code for both the server and multiple clients. The server code establishes a socket and listens for incoming connections, while client code connects to the server socket and sends/receives data. There are 4 questions with variations in the code for different client-server interactions, such as the server receiving and returning messages in various formats.

Uploaded by

Imaad Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

APP Assignment -1

Network Paradigm

IMAAD ZAFFAR KHAN

RA1811003010850

Sec F2

QUESTION 1:
Servercode
import socket

s=socket.socket()

print("socket created")

host=socket.gethostname()

port=6666

s.bind((host,port))

s.listen(11)

print("waiting")

while True:

cl,addr=s.accept()

print("connected with client",addr)

command=input("enter command:")

cl.send(bytes(command,'utf-8'))

cl.close()

output :servercode
socket created

waiting

connected with client ('127.0.0.1', 57231)

enter command:vasanth
CLIENTCODE:
import socket

cl=socket.socket()

host=socket.gethostname()

port=6666

cl.connect((host,port))

k=cl.recv(1024).decode()

print(k)

Client output:
vasanth

Question 2:
Servercode:

import socket

s=socket.socket()

print("socket created")

host=socket.gethostname()

port=6666

s.bind((host,port))

s.listen(11)

print("waiting")

while True:

cl,addr=s.accept()

print("connected with client",addr)

k=cl.recv(1024).decode()

cl.send(bytes(k.upper(),'utf-8'))

cl.close()

Serveroutput:

socket created

waiting

connected with client ('127.0.0.1', 57809)


Clientcode:

import socket

cl=socket.socket()

host=socket.gethostname()

port=6666

cl.connect((host,port))

nam=input("Enter the name:")

cl.send(bytes(nam,'utf-8'))

m=cl.recv(1024).decode()

print("uppercase of it is:",m)

Clientoutput:

Enter the name:vasanth

uppercase of it is: VASANTH

Question 3:
ServerCode:

import socket
s=socket.socket()
print("socket created")
host=socket.gethostname()
port=6666
s.bind((host,port))
s.listen(11)
print("waiting")
while True:
cl,addr=s.accept()
print("connected with client",addr)
k=cl.recv(1024).decode()
if(k=='PING' or k=='ping'):
m='SERVER: PONG'
else:
m='SERVER DROPPED'
cl.send(bytes(m,'utf-8'))
cl.close()
Serveroutput:
socket created
waiting
connected with client ('192.168.43.88', 58628)
connected with client ('192.168.43.88', 58632)
Clientcode:

import socket
cl=socket.socket()
host=socket.gethostname()
port=6666
cl.connect((host,port))
nam=input("Enter the message:")
cl.send(bytes(nam,'utf-8'))
print("CLIENT:",nam)
p=cl.recv(1024).decode()
print(p)
Clientoutput:
Enter the message:ping
CLIENT: ping
SERVER: PONG

Question 4:
ServerCode:

import socket
from threading import Thread
def thread():
while True:
data = conn.recv(1024)
print('Client Request :' + data.decode())
if data == 'quit' or not data:
print("Server Exiting")
break
data = input('Server Response:')
conn.sendall(data.encode())

host = socket.gethostname()
port = 3333
s = socket.socket()
s.bind((host,port))
s.listen(5)

print("Waiting for clients...")


while True:
conn,addr = s.accept()
print("Connected by ", addr)
pr = Thread(target=thread)
pr.start()

conn.close()
Client1:
import socket
s = socket.socket()
host = socket.gethostname()
port = 3333
s.connect((host,port))
s.send(bytes('ram','utf-8'))
while True:
data = s.recv(1024)
if data == 'quit' or not data:
print("Quiting")
break
else:
print("Server: ",data.decode())
msg = input("Client: ")
s.send(msg.encode())
s.close()
Client2:
import socket
s = socket.socket()
host = socket.gethostname()
port = 3333
s.connect((host,port))
s.send(bytes('ravi','utf-8'))
while True:
data = s.recv(1024)
if data == 'quit' or not data:
print("Quiting")
break
else:
print("Server: ",data.decode())
msg = input("Client: ")
s.send(msg.encode())
s.close()
Client3:
import socket
s = socket.socket()
host = socket.gethostname()
port = 3333
s.connect((host,port))
s.send(bytes('rakesh','utf-8'))
while True:
data = s.recv(1024)
if data == 'quit' or not data:
print("Quiting")
break
else:
print("Server: ",data.decode())
msg = input("Client: ")
s.send(msg.encode())
s.close()

OUTPUTS
1. Client and Server:
Question 2:
Question 3:
Question 4:

Client 1

Client 2
Client 3

You might also like