Write a python script for each one of (2) and (3): (2) Multi-Threaded Server Cur
ID: 3596116 • Letter: W
Question
Write a python script for each one of (2) and (3):
(2) Multi-Threaded Server Currently, the web server handles only one HTTP request at a time. Implement a multithreaded server that is capable of serving multiple requests simultaneously. Using threading, first create a main thread in which your modified server listens for clients at a fixed port. When it receives a TCP connection request from a client, it will set up the TCP connection through another port and services the client request in a separate thread. There will be a separate TCP connection in a separate thread for each request/response pair (3) HTTP Client Instead of using a browser, write your own HTTP client to test your server. Your client will connect to the server using a TCP connection, send an HTTP request to the server, and display the server response as an output. You can assume that the HTTP request sent is a GET method The client should take command line arguments specifying the server IP address or host name, the port at which the server is listening, and the path at which the requested object is stored at the server. The following is an input command format to run the client client.py server host server port filenameExplanation / Answer
2.
Following is the code for Multi-threaded Server in Python:
import socket
from threading import Thread
from SocketServer import ThreadingMixIn
# Multithreaded Python server
class MultiThread(Thread):
def __init__(self,ip,port):
Thread.__init__(self)
self.ip = ip
self.port = port
print "New thread for ip: " + ip + ":" + str(port)
def run(self):
while True :
data = conn.recv(2048)
print "Received data:", data
msg = raw_input("Multithreaded Python server ; Enter Response:")
if msg == 'exit':
break
conn.send(msg)
TCP_IP = '0.0.0.0'
TCP_PORT = 2004
BUFFER_SIZE = 20
tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpServer.bind((TCP_IP, TCP_PORT))
ar = []
while True:
tcpServer.listen(4)
print "Waiting for connection..."
(conn, (ip,port)) = tcpServer.accept()
tread = MultiThread(ip,port)
tread.start()
ar.append(tread)
for i in ar:
i.join()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.