Professional Documents
Culture Documents
20BIT0444 Ashutosh Raj Shrestha
20BIT0444 Ashutosh Raj Shrestha
20BIT0444 Ashutosh Raj Shrestha
Implement date server and client in python using TCP sockets. The client will request the server to
send the today’s date and server will send in dd/mm/yyyy format.
CODE:
serverq1.py
import socket
import datetime
s=socket.socket()
host=socket.gethostname()
port=123
s.bind((host,port))
s.listen(5)
current_day = datetime.date.today()
formatted_date = datetime.date.strftime(current_day,"%d/%m/%Y")
string_date=str(formatted_date)
while True:
c,a=s.accept()
c.send (string_date.encode())
c.close()
clientq1.py
import socket
host=socket.gethostname()
port=123
s= socket.socket()
s.connect((host,port))
print (s.recv(1024).decode())
s.close()
OUTPUT
Write a client-server program, where the client will send a six digit number to the server and the
server will add the first, third and fifth position values and the result will be sent back to the client.
CODE
serverq2.py
import socket
s=socket.socket()
host=socket.gethostname()
port=12345
s.bind((host,port))
s.listen(5)
while True:
c,a=s.accept()
new=(c.recv(1024).decode())
answer=int(new[0])+int(new[2])+int(new[4])
new_answer=str(answer)
c.send (new_answer.encode())
c.close()
clientq2.py
import socket
host=socket.gethostname()
port=12345
s= socket.socket()
s.connect((host,port))
number=input("Enter a number")
new_number=str(number)
s.send(new_number.encode())
print (s.recv(1024).decode())
s.close()
OUTPUT
Write a TCP/IP based client-server python code for a “Close Tender”. The tendering process proceeds
as follows: The server will think for an amount within 5000-6000 randomly which will be printed as
“Proposed Amount” at the server program. Randomly take the “Tender amount” for five users such as
User 1, User 2, User 3, User 4 and User 5 in the client side and send those values to the server. The
server will compare all user’s value with the “Proposed Amount” to find out the closest one, then, it
will send a message “Congratulations!!! (****User name) got the Tender” to the client.
CODE
serverq3.py
import socket
import random
s= socket.socket()
host=socket.gethostname()
port=12375
s.bind((host,port))
s.listen(5)
tender_amount=random.randrange(5000,6000)
print(tender_amount)
minimum=4040
while True:
c,a=s.accept()
user=(c.recv(1024).decode())
us=eval(user)
for i in range(5):
ex=us[i]
difference=abs(tender_amount-ex)
if(difference<minimum):
minimum=difference
counter=i
c.send(winner.encode())
c.close()
clientq3.py
import socket
import random
host=socket.gethostname()
port=12375
s= socket.socket()
s.connect((host,port))
numbers=[]
for i in range(5):
ran=random.randrange(5000,6000)
numbers.append(ran)
n=str(numbers)
s.send(n.encode())
print(s.recv(1024))
s.close()
OUTPUT