Домашнее задание -Прокси-сервер Python

Для упражнения по программированию(из Computer Networking :A Top -Down Approach (6th Edition )Kurose and Ross), мы пытаемся разработать простой прокси-сервер на питоне.

Нам дали следующий код, везде, где он говорит #Fill in start.... #Fill in end., именно там нам нужно написать код. Мой конкретный вопрос и попытки будут ниже этого оригинального фрагмента.

Нам нужно запустить сервер Python с:python proxyserver.py [server_ip]затем перейдите к localhost:8888/google.com, и он должен работать, когда мы закончим.

from socket import *
import sys
if len(sys.argv) <= 1:
  print 'Usage : "python ProxyServer.py server_ip"\n[server_ip : It is the IP Address Of Proxy Server'
  sys.exit(2)

# Create a server socket, bind it to a port and start listening 
tcpSerSock = socket(AF_INET, SOCK_STREAM)
# Fill in start.
# Fill in end.

while 1:
  # Strat receiving data from the client
  print 'Ready to serve...'
  tcpCliSock, addr = tcpSerSock.accept()
  print 'Received a connection from:', addr 
  message = # Fill in start. # Fill in end. print message

  # Extract the filename from the given message print message.split()[1]
  filename = message.split()[1].partition("/")[2] print filename
  fileExist = "false"
  filetouse = "/" + filename
  print filetouse
  try:
    # Check wether the file exist in the cache
    f = open(filetouse[1:], "r")
    outputdata = f.readlines()
    fileExist = "true"

    # ProxyServer finds a cache hit and generates a response message     
    tcpCliSock.send("HTTP/1.0 200 OK\r\n") 
    tcpCliSock.send("Content-Type:text/html\r\n")
    # Fill in start.
    # Fill in end.
      print 'Read from cache'
  # Error handling for file not found in cache
except IOError:
  if fileExist == "false":
    # Create a socket on the proxyserver
    c = # Fill in start. # Fill in end. 
    hostn = filename.replace("www.","",1)
    print hostn
      try:
        # Connect to the socket to port 80
        # Fill in start.
        # Fill in end.

        # Create a temporary file on this socket and ask port 80 for the file requested by the client
        fileobj = c.makefile('r', 0)
        fileobj.write("GET "+"http://" + filename + "HTTP/1.0\n\n")

        # Read the response into buffer
        # Fill in start.
        # Fill in end.

        # Create a new file in the cache for the requested file. 
        # Also send the response in the buffer to client socket and the corresponding file in the cache
        tmpFile = open("./" + filename,"wb")
        # Fill in start.
        # Fill in end.
      except:
        print "Illegal request"
    else:
      # HTTP response message for file not found 
      # Fill in start.
      # Fill in end.
  # Close the client and the server sockets
    tcpCliSock.close()
# Fill in start.
# Fill in end.

Где написано:

# Create a socket on the proxyserver
c = # Fill in start. # Fill in end. 

Я пытался:

c = socket(AF_INET, SOCK_STREAM)

Кажется, так вы создаете сокет, тогда для подключения к порту 80 хоста у меня есть:

c.connect((hostn, 80))

Здесь hostnправильно google.comв соответствии с некоторыми имеющимися у меня локальными операторами печати.В следующем разделе, который я должен заполнить, говорится #Read response into buffer, но я не очень понимаю, что это значит. Я предполагаю, что это как-то связано с fileobj, созданным чуть выше.

Заранее спасибо, пожалуйста, дайте мне знать, если я пропустил что-то, что я должен добавить.

ОБНОВЛЕНИЕ

Мой текущий код можно найти здесь, чтобы увидеть, что я пробовал:

https://github.com/ardavis/Computer-Networks/blob/master/Lab%203/ProxyServer.py

8
задан ardavis 25 September 2013 в 11:18
поделиться