fork download
  1. from socket import *
  2. import sys
  3.  
  4. if len(sys.argv) <= 1:
  5. print('Usage : "python ProxyServer.py server_ip"\n[server_ip : It is the IP Address Of Proxy Server')
  6. sys.exit(2)
  7.  
  8. # Create a server socket, bind it to a port and start listening
  9. tcpSerSock = socket(AF_INET, SOCK_STREAM)
  10. # TODO start.
  11. tcpSerPort = 7777
  12. tcpSerSock.bind((sys.argv[1], tcpSerPort))
  13. tcpSerSock.listen(1)
  14. # TODO end.
  15. while 1:
  16. # Strat receiving data from the client
  17. print('Ready to serve...')
  18. tcpCliSock, addr = tcpSerSock.accept()
  19. print('Received a connection from:', addr)
  20.  
  21. # Receive request from the client
  22. # TODO start.
  23. message = tcpCliSock.recv(1024).decode('utf-8')
  24. # TODO end.
  25. print(message)
  26.  
  27. # Extract the filename from the given message
  28. print(message.split()[1])
  29. filename = message.split()[1].partition("/")[2]
  30. print(filename)
  31. fileExist = "false"
  32. filetouse = "/" + filename
  33. print(filetouse)
  34. try:
  35. # Check wether the file exist in the cache
  36. f = open(filetouse[1:], "r")
  37. outputdata = f.readlines()
  38. fileExist = "true"
  39.  
  40. # ProxyServer finds a cache hit and generates a response message
  41. # Send the file data to the client
  42. tcpCliSock.send(b"HTTP/1.0 200 OK\r\n")
  43. tcpCliSock.send(b"Content-Type: text/html\r\n")
  44. # TODO start.
  45. for i in range(0, len(outputdata)):
  46. tcpCliSock.send(outputdata[i].encode('utf-8'))
  47. tcpCliSock.send(b'\r\n')
  48. # TODO end.
  49.  
  50. print('Read from cache')
  51. # Error handling for file not found in cache
  52. except IOError:
  53. if fileExist == "false":
  54. # Create a socket on the proxyserver
  55. c = socket(AF_INET, SOCK_STREAM)
  56. hostn = filename.replace("www.","",1)
  57. print(hostn)
  58. try:
  59. # Connect to the socket to port 80
  60. # TODO start.
  61. c.connect((hostn, 80))
  62. # TODO end.
  63.  
  64. # Create a temporary file on this socket and ask port 80 for the file requested by the client
  65. fileobj = c.makefile('rw', None)
  66. fileobj.write("GET "+"http://" + filename + " HTTP/1.0\n\n")
  67. # Read the response into buffer
  68. # TODO start.
  69. buf = fileobj.read()
  70. # TODO end.
  71.  
  72. # Create a new file in the cache for the requested file.
  73. # Also send the response in the buffer to client socket and the corresponding file in the cache
  74. tmpFile = open("./" + filename,"wb")
  75. # TODO start.
  76. for i in range(0, len(buf)):
  77. tmpFile.write(buf[i])
  78. tcpCliSock.send(tmpFile)
  79. # TODO end.
  80. except:
  81. print("Illegal request")
  82. else:
  83. # HTTP response message for file not found
  84. # Fill in start.
  85. tcpCliSock.send(b'HTTP/1.0 404 Not Found\r\n')
  86. tcpCliSock.send(b"Content-Type: text/html\r\n\r\n")
  87. # Fill in end.
  88. # Close the client sockets
  89. tcpCliSock.close()
  90. # Close the server socket
  91. # TODO start.
  92. tcpSerSock.close()
  93. # TODO end.
  94.  
Runtime error #stdin #stdout 0.02s 9472KB
stdin
Standard input is empty
stdout
Usage : "python ProxyServer.py server_ip"
[server_ip : It is the IP Address Of Proxy Server