The Python Proxy Server not functioning

I have the below code written in Python for a proxy server (which should be able to cache web pages. It is a very simple proxy server which only understands simple GET-requests, but is able to handle all kinds of objects–not just HTML pages, but also images), but when I try to run it it doesnt do anything. I have activated the proxy settings on windows. Here is the code below:
enter image description here


`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 OfProxy 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.
tcpSerSock.bind((sys.argv[1], 8888))
tcpSerSock.listen(5)
# 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 = tcpCliSock.recv(1024).decode() # Fill in start.
  print(message)               # Fill in end. 

  # 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 OKrn") 
    tcpCliSock.send("Content-Type:text/htmlrn")
    # Fill in start.
    for line in outputdata:
        tcpCliSock.send(line.encode())
    #f.close()
    # 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 = socket(AF_INET, SOCK_STREAM)# Fill in start. # Fill in end. 
        hostn = filename.replace("www.","",1)
        print (hostn)
        try:
            # Connect to the socket to port 80
            # Fill in start.
            c.connect((hostn, 80))
            # 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.0nn")

            # Read the response into buffer
            # Fill in start.
            buffer = fileobj.read()
            # 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.
            tmpFile.write(buffer)
            tmpFile.close()
            tcpCliSock.send(buffer.encode())
            # Fill in end.
        except:
                print ("Illegal request")
else:
    # HTTP response message for file not found 
    # Fill in start.
    tcpCliSock.send("HTTP/1.0 404 Not Foundrn".encode())
    tcpCliSock.send("Content-Type:text/htmlrn".encode())
    tcpCliSock.send("rn".encode())
    tcpCliSock.send("<html><body><h1>404 Not Found</h1></body></html>rn".encode())
    # Fill in end.
    # Close the client and the server sockets
    tcpCliSock.close()
# Fill in start.
tcpSerSock.close()
# Fill in end’

Any help would be much appreciated

I tried the code on my computer, using cmd and i attached a

picture of the result. It doesnt seem to contiue

New contributor

The Choumans is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật