Devoirs - Python Serveur Proxy

Pour un exercice de programmation (à partir de l'Ordinateur de Réseau: Une Approche Top-Down (6e Édition) par Kurose et Ross), nous essayons de développer un simple serveur proxy en python.

On nous a donné le code suivant, où il est dit #Fill in start. ... #Fill in end. c'est là que nous avons besoin d'écrire du code. Ma question et tente sera en dessous de l'original de ce fragment de code.

Nous avons besoin pour démarrer le python serveur avec: python proxyserver.py [server_ip] puis accédez à localhost:8888/google.com et cela devrait fonctionner quand nous aurons fini.

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.

Où il est dit:

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

J'ai essayé:

c = socket(AF_INET, SOCK_STREAM)

Cela semble être une façon de créer un socket, puis pour la connexion vers le port 80 de l'hôte, j'ai:

c.connect((hostn, 80))

Ici, hostn est correctement google.com selon certains locaux des instructions d'impression que j'ai. La section suivante pour moi de remplir leur dit #Read response into buffer mais je ne comprends pas vraiment ce que cela signifie. Je présume qu'il a quelque chose à voir avec la fileobj qui est créé juste au-dessus.

Merci à l'avance, s'il vous plaît laissez-moi savoir si j'ai manqué quelque chose que je dois ajouter.

Mise à JOUR

Mon code actuel peut être trouvé ici pour voir ce que j'ai essayé:

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

Pour info, socket.create_connection est plus facile.
Je n'ai jamais utilisé python avant, donc j'ai encore beaucoup à apprendre. Je vais prendre un coup d'oeil, je sais juste que nous avons à faire la mission comme prévu. Merci!
Hey, j'ai reçu une plainte informelle à ce sujet - peut-être une bonne idée de prévoir l'attribution de l'exercice de code, si vous pouvez vous souvenir d'où il est originaire.
Fait, merci pour l'astuce. Désolé à ce sujet.

OriginalL'auteur ardavis | 2012-07-29