Problem 3 Problem 3: Socket Programming 1) Rewrite the Python code below using U
ID: 3726549 • Letter: P
Question
Problem 3
Problem 3: Socket Programming 1) Rewrite the Python code below using UDP socket programming rather than TCP socket (25 points) programming a. This means we are pretending that someone has written a web browser that will also use UDP-you assume this exists and do not worry about it at all. If you do not remember the exact method (function) or parameter names for UDP, feel free to make up your own names, and be sure to explain, in a comment, what you expect the method to do b. 2) Add programming comments so that any reader of your rewritten program, especially those who have not ever done socket programming (but have had CSC 111, so do know Python basics), will understand each line, or group of lines, of the program # CSC 249 WebSocket.py from socket import* serversocket = socket (AF INET, SOCK STREAM) serverHost ' localhost ' recvBuffer 1024 ServerPort = 1602 serverSocket.bind( (serverHost, serverPort)) serverSocket.listen (1) while True: connectionSocket, addr- serverSocket.accept () message - connectionSocket.recv (recvBuffer) filename = message. split ( ) [ 1 ] [ 1 : ] if path.exists(filename): fopn - open(filename) filedata = fopn.read ( ) connectionSocket.send ( "HTTP/1.1 200 OK ") for i in range (0, len (filedata)): connectionSocket.send(filedata[i]) connectionSocket.send(" " ) connectionSocket.close() else: connectionSocket.send( "HTTP/1.1 404 Not Found ") connectionSocket.send("404 Not Found ") connectionSocket.close() serverSocket.close ()Explanation / Answer
Below is the socket program to use UDP :
from socket import *
import sys
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverHost = 'localhost'
recvBuffer = 1024
serverPort = 1602
serverSocket.bind(serverHost, serverPort)
while True:
message, addr = serverSocket.recvfrom(recvBuffer)
filename = message.split()[1][1:]
if path.exists(filename):
fopn = open(filename)
filedata = fopn.read()
serverSocket.sendto("HTTP/1.1 200 OK ",addr)
for i in range(0, len(filedata[i])
serverSocket.sendto(filedata[i],addr)
serverSocket.sendto(" ",addr)
serverSocket.close();
else:
serverSocket.sendto("HTTP/1.1 404 Not Found ",addr)
serverSocket.sendto("<html><head></head><body><h1>404 Not Found</h1></body></html> ",addr)
serverSocket.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.