Hausaufgaben - Python-Proxy-Server

Für eine Programmierung (aus-Computer Networking: A Top-Down Approach (6th Edition) von Kurose und Ross), versuchen wir die Entwicklung eines einfachen proxy-server in python.

Erhielten wir den folgenden code ein, wo immer es sagt #Fill in start. ... #Fill in end. das ist, wo wir brauchen, um code zu schreiben. Meine spezifische Frage ist und versucht wird, unter dem original-Codefragment.

Wir müssen starten Sie den python-server mit: python proxyserver.py [server_ip] navigieren Sie dann zu localhost:8888/google.com und es sollte funktionieren, wenn wir fertig sind.

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.

Wo es heißt:

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

Habe ich versucht:

c = socket(AF_INET, SOCK_STREAM)

Diese zu sein scheint, wie Sie ein socket erstellen, dann für die Verbindung zu port 80 des Hosts, die ich habe:

c.connect((hostn, 80))

Hier hostn ist richtig google.com nach einigen lokalen print-Anweisungen, die ich habe. Der nächste Abschnitt für mich zu füllen, sagt #Read response into buffer aber ich weiß nicht wirklich verstehen, was das bedeutet. Ich vermute, es hat etwas zu tun mit der fileobj, die erstellt wird, nur oben.

Vielen Dank im Voraus, bitte lassen Sie mich wissen, wenn ich nichts übersehen habe, sollte ich hinzufügen.

UPDATE

Mein aktuelle code kann hier eingesehen werden, um zu sehen, was ich versucht habe:

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

  • FYI, socket.create_connection einfacher ist.
  • Ich habe noch nie verwendet python vor, so ich habe noch eine Menge zu lernen. Ich werde einen Blick drauf werfen, ich weiß nur, wir haben zu tun, die Zuweisung als gelegt. Danke!
  • Hey, ich habe eine informelle Beschwerde über diese - vielleicht eine gute Idee, attribution, die für die Ausübung code, wenn Sie sich erinnern können, wo es ursprünglich kam.
  • Erledigt, danke für den Tipp. Tut mir Leid, dass.
InformationsquelleAutor ardavis | 2012-07-29
Schreibe einen Kommentar