-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_server_multi_program.py
More file actions
62 lines (48 loc) · 1.79 KB
/
Copy pathproxy_server_multi_program.py
File metadata and controls
62 lines (48 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import socket, sys
from multiprocessing import Process
HOST = ""
PORT = 8001
BUFFER_SIZE = 1024
def get_remote_ip(host):
print(f'Getting IP for {host}')
try:
remote_ip = socket.gethostbyname( host )
except socket.gaierror:
print('Hostname could not be resolved. Exiting')
sys.exit()
print(f'Ip address of {host} is {remote_ip}')
return remote_ip
def handle_request(conn, addr, proxy_end):
send_full_data = conn.recv(BUFFER_SIZE)
print(f"Sending received data {send_full_data} to google")
proxy_end.sendall(send_full_data)
proxy_end.shutdown(socket.SHUT_WR)
data = proxy_end.recv(BUFFER_SIZE)
print(f'Sending received data {data} to client')
conn.send(data)
def main():
host = 'www.google.com'
port = 80
#create start socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as start:
print("Create proxy server")
start.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #q3, allows to reuse same bind port
start.bind((HOST, PORT))
start.listen(1) #make socket listen
#listen forever for connections
while True:
conn, addr = start.accept() #accept incoming connections
print("Connected by ", addr)
#create end socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as end:
print("Connecting to Google")
remote_ip = get_remote_ip('www.google.com')
end.connect((remote_ip , port))
# handle multiple request
p = Process(target=handle_request, args=(conn, addr, proxy_end))
p.daemon = True
p.start()
print("Started process ", p)
conn.close()
if __name__ == "__main__":
main()