Manual PDF
Manual PDF
AIM:
To write a python program for simple client-server using UDP.
PROCEDURE:
SERVER SIDE:
CLIENT SIDE:
PROGRAM:
SERVER SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',7100))
data,addr=s.recvfrom(1024)
print(data)
CLIENT SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto(str.encode("HII SERVER"),('127.0.0.1',7100))
OUTPUT:
SERVER SIDE:
>>>exec(open(“R:\\New folder\\server.py”).read())
B’HII SERVER’
CLIENT SIDE:
>>>exec(open(“R:\\New folder\\client.py”).read())
RESULT:
Thus the program for client-server using UDP is implemented.
ECHO CLIENT SERVER IMPLEMENTATION USING UDP
AIM:
To write a python program for Echo client-server communication using UDP.
PROCEDURE:
SERVER SIDE:
Step5:Receive the data packets from the client along with its address.
CLIENT SIDE:
PROGRAM:
SERVER SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',6011))
print("server is waiting")
data,addr=s.recvfrom(1024)
print(data,addr)
s.sendto(data,addr)
CLIENT SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto(str.encode("hii"),('127.0.0.1',6011))
data,addr=s.recvfrom(1024)
print(data,addr)
OUTPUT:
SERVER SIDE:
>>>exec(open(“R:\\New folder\\server.py”).read())
server is waiting
b’hii’(‘127.0.0.1’,49161)
CLIENT SIDE:
>>>exec(open(“R:\\New folder\\client.py”).read())
b’hii’(‘127.0.0.1’,6011)
RESULT:
Thus the program for Echo client-server communication using UDP is implemented.
HALF DUPLEX COMMUNICATION USING TCP
AIM:
To write a python program for half duplex communication using TCP.
PROCEDURE:
SERVER SIDE:
CLIENT SIDE:
PROGRAM:
SERVER SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('127.0.0.1',6012))
s.listen(1)
client,addr=s.accept()
while True:
data=client.recv(1024)
print(data)
sentence=input(">>")
client.send(str.encode(sentence))
if "quit" in str(sentence):
break
client.close()
CLIENT SIDE:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('127.0.0.1',6012))
while True:
sen=input(">>")
s.send(str.encode(sen))
data=s.recv(1024)
print(data)
if "quit" in str(data):
break
OUTPUT:
SERVER SIDE:
>>>exec(open(“R:\\New folder\\server.py”).read())
b’hii’
>>hello
>>fine
CLIENT SIDE:
>>>exec(open(“R:\\New folder\\client.py”).read())
>>hii
b’hello’
b’fine’
RESULT:
Thus the half duplex communication using TCP is implemented.
FULL DUPLEX COMMUNICATION USING UDP
AIM:
To write a python program for full duplex communication.
PROCEDURE:
SERVER SIDE:
Step6:Communication between clients and the server will record the date and time transmitted
between clients.
CLIENT SIDE:
Step5:Server again retransmits the data to other clients connected to server using threading.
Step6:Print the data in clients, such that there is full duplex communication.
Step7:If the message is q, then client stops sending data to the server.
PROGRAM:
SERVER SIDE:
import socket
import time
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',7000))
clients=[]
s.setblocking(0)
quitting=False
try:
data,addr=s.recvfrom(1024)
if 'q' in str(data):
quitting=True
clients.append(addr)
print(time.ctime(time.time())+str(addr)+":"+str(data))
s.sendto(data,client)
except:
pass
s.close()
CLIENT SIDE:
import socket
import time
import threading
shutdown=False
tlock=threading.Lock()
def receiving(name,sock):
tlock.acquire()
try:
while True:
data,addr=sock.recvfrom(1024)
print(data)
except:
pass
finally:
tlock.release()
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',0))
s.setblocking(0)
rT=threading.Thread(target=receiving,args=("Recvthread",s))
rT.start()
alias=input("Name:")
ali=str.encode(alias+":")
message=input(alias+"->")
msg=str.encode(message)
d=(ali+msg)
s.sendto(d,('127.0.0.1',7000))
tlock.acquire()
message=input(alias+"->")
msg=str.encode(message)
tlock.release()
shutdown=True
rT.join()
s.close()
OUTPUT:
SERVER SIDE:
>>>exec(open(“R:\\New folder\\server.py”).read())
CLIENT SIDE:
>>>exec(open(“R:\\New folder\\client.py”).read())
Name:janu
Janu->hii
Janu->
b’janu:hii’
b’jansi:hello’
janu->who is this
>>>exec(open(“R:\\New folder\\client.py”).read())
Name:jansi
Jansi->
b’hii
jansi->hello
b’janu:hii’
b’jansi:hello’
b’janu:who is this?
RESULT:
Thus the full duplex communication is established.
IMPLEMENTATION OF SIMPLE STOP AND WAIT PROTOCOL
AIM:
To write a python program to send and receive frame using Stop and Wait protocol.
PROCEDURE:
SENDER:
Step4:Send a frame.
Step5:After transmitting one packet, the sender waits for an acknowledgement before transmitting next
one.
RECEIVER:
PROGRAM:
SENDER:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',6009))
while True:
data=input(">>")
s.sendto(str.encode(data),('127.0.0.1',6014))
m,a=s.recvfrom(1024)
print("EXPECTING FRAME:"+str(m))
s.close()
RECEIVER:
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('127.0.0.1',6014))
count='0'
while True:
acknowledge=False
ack,ad=s.recvfrom(1024)
print("FRAME RECEIVED:"+str(ack))
if "0" in str(ack):
count='1'
count='0'
else:
break
s.sendto(str.encode(count),ad)
acknowledge=True
s.close()
OUTPUT:
SENDER:
>>>exec(open(“R:\\New folder\\sender.py”).read())
>>1
EXPECTING FRAME:b’0’
>>0
EXPECTING FRAME:b’1’
>>1
RECEIVER:
>>>exec(open(“R:\\New folder\\sender.py”).read())
FRAME RECEIVED:b’1’
FRAME RECEIVED:b’0’
RESULT:
Thus the frame has been sent and received using stop and wait protocol.
IMPLEMENTATION OF ARP PROTOCOL
AIM:
To write a python program to simulate Address Resolution Protocol.
PROCEDURE:
Step1:Create an array to store the IP and its corresponding MAC address.
PROGRAM:
d={}
for i in range(2):
d[ip]=mac
print()
print(d)
print(d[s])
OUTPUT:
>>>exec(open(“R:\\New folder\\arp.py”).read())
{’10.1.24.121’:’E0-CA-94-CB-A4-D8’,’10.1.24.122’:’E0-C7-94-CB-A4-D7’}
RESULT:
Thus the Address Resolution Protocol is simulated.
FIND THE ADDRESS SPACE,FIRST ADDRESS,LAST ADDRESS USING
SUBNETTING IN CIDR
AIM:
To write a python program to calculate first address, last address and address space of given IP.
PROCEDURE:
Step1:Import ipaddress module and math module.
Step5:Using IPV4 network, list of all addresses from first to last are stored in array.
Step6:Print n[0] and n[-1] to get the first and last address.
PROGRAM:
import ipaddress
import math
i=ip.split("/")
p=i[1]
l=32-int(p)
val=math.pow(2,int(l))
n=ipaddress.IPv4Network(ip)
first,last=n[1],n[-1]
print("ADDRESS BLOCK",int(val))
print("FIRST ADDRESS",first)
print("LAST ADDRESS",last)
OUTPUT:
>>>exec(open(“R:\\New folder\\subnet.py”).read())
RESULT:
Thus the address space,last address and first addresses are calculated from the given ip.
IMPLEMENTATION OF HTTP SOCKET
AIM:
To write a python program for webpage upload and download using http socket.
PROCEDURE:
UPLOADING:
DOWNLOADING:
PROGRAM:
UPLOADING:
class Serv(BaseHTTPRequestHandler):
def do_GET(self):
if self.path==(r'/'):
self.path=(r'C:/new\file.html')
try:
file_to_open=open(self.path[2:]).read()
self.send_response(200)
except:
file_to_open="FILE ERROR"
self.send_response(404)
self.end_headers()
self.wfile.write(bytes(file_to_open,'utf-8'))
httpd=HTTPServer(('127.0.0.1',8000),Serv)
httpd.serve_forever()
HTML FILE:
<html>
<head>
<title>welcome</title>
</head>
<body>
<h3>WELCOME</h3>
</body>
</html>
DOWNLOADING:
import urllib.requesturllib.request.urlretrieve("https://www.govtrack.us/congress/votes/115-
2017/h200/diagram","angl.jpg")
OUTPUT:
UPLOAD:
>>>exec(open(“R:\\New folder\\upload.py”).read())
DOWNLOAD:
>>>exec(open(“R:\\New folder\\download.py”).read())
RESULT:
Thus the webpage upload and download are done using http socket.