Last active
September 22, 2019 14:32
-
-
Save tlkh/4c53bdec54fccf516c54002e58840ec0 to your computer and use it in GitHub Desktop.
not network answer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 50.012 network lab 1 | |
# Adapted from K & R's original code | |
from socket import * | |
import sys | |
import _thread as thread | |
import os | |
import functools | |
proxy_port = 8079 | |
@functools.lru_cache(maxsize=1024) | |
def proxy_cache(webServer, port, message): | |
# Create a socket on the proxyserver | |
c = socket(AF_INET, SOCK_STREAM) | |
print("[i] Created:", c) | |
c.connect((webServer, port)) | |
print("[i] Connected:", webServer, port) | |
webServer = bytes(webServer, encoding="ascii") | |
message = bytes(message, encoding="ascii") | |
print("[i] Send message:", message) | |
c.sendall(message) | |
chunks = [] | |
print("[i] Start recv...", end=" ") | |
while True: | |
buff = c.recv(1024) | |
print(len(buff), end=" ... ") | |
if len(buff) < 1: | |
break | |
else: | |
chunks.append(buff) | |
# Close the socket after use | |
c.close() | |
buff = b"".join(chunks) | |
return buff | |
def client_thread(tcpCliSock): | |
tcpCliSock.settimeout(5.0) | |
try: | |
message = tcpCliSock.recv(4096).decode("ascii") | |
except: | |
print("error", str(sys.exc_info()[0])) | |
tcpCliSock.close() | |
return | |
print("[i] Recieved message:", message) | |
# Extract the following info from the received message | |
# webServer: the web server's host name | |
# resource: the web resource requested | |
# file_to_use: a valid file name to cache the requested resource | |
# Assume the HTTP reques is in the format of: | |
# GET http://www.ucla.edu/img/apple-touch-icon.png HTTP/1.1\r\n | |
# Host: www.ucla.edu\r\n | |
# User-Agent: ..... | |
# Accept: ...... | |
msgElements = message.split() | |
if len(msgElements) < 5: | |
print("[e] Non-supported request: ", msgElements) | |
tcpCliSock.close() | |
return | |
if msgElements[0].upper() != 'GET' or msgElements[3].upper() != 'HOST:': | |
print("[e] Non-supported request:", msgElements[0], msgElements[3]) | |
tcpCliSock.close() | |
return | |
resource = msgElements[1].replace("http://", "", 1) | |
webServer = msgElements[4] | |
port = 80 | |
print("[i] webServer:", webServer) | |
print("[i] resource:", resource) | |
message = message.replace("Connection: keep-alive", "Connection: close") | |
if ":443" in resource: | |
port = 443 | |
print("[e] Sorry, so far our program cannot deal with HTTPS yet") | |
tcpCliSock.close() | |
return | |
print("[i] Ask for the buffer...") | |
buffer = proxy_cache(webServer, port, message) | |
print("\n[i] Got the buffer:", proxy_cache.cache_info()) | |
tcpCliSock.send(buffer) | |
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 | |
tcpSerSock.bind((sys.argv[1], proxy_port)) | |
tcpSerSock.listen(1) | |
# Fill in end | |
print('[i] Proxy ready to serve at', sys.argv[1], proxy_port) | |
try: | |
while True: | |
# Start receiving data from the client | |
tcpCliSock, addr = tcpSerSock.accept() | |
print('[i] Received a connection from:', addr) | |
# the following function starts a new thread, taking the function name as the first argument, and a tuple of arguments to the function as its second argument | |
thread.start_new_thread(client_thread, (tcpCliSock, )) | |
except KeyboardInterrupt: | |
print('[i] End') | |
finally: | |
# Close the socket after use | |
tcpSerSock.close() | |
tcpCliSock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment