-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_server.py
More file actions
44 lines (37 loc) · 1.4 KB
/
Copy pathproxy_server.py
File metadata and controls
44 lines (37 loc) · 1.4 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
import socket
HOST = ""
PORT = 8001
BUFFER_SIZE = 1024
def get_remote_ip(host):
print('Getting IP for ', host)
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror:
print('Hostname could not be resolved. Exiting')
sys.exit()
print('Ip address of ' +host+ ' is '+remote_ip)
return remote_ip
def main():
host = 'www.google.com'
port = 80
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as proxy_start:
print("Starting proxy server")
proxy_start.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
proxy_start.bind((HOST, PORT))
proxy_start.listen(1)
while True:
conn, addr = proxy_start.accept()
print("Connected by", addr)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as proxy_end:
print("Connecting to Google")
remote_ip = get_remote_ip(host)
proxy_end.connect((host, port))
send_full_data = conn.recv(BUFFER_SIZE)
print("Sending received data " + send_full_data.decode("utf-8") + " to google")
proxy_end.sendall(send_full_data)
proxy_end.shutdown(socket.SHUT_WR)
data = proxy_end.recv(BUFFER_SIZE)
print("Sending received data " +data+ " to client")
conn.send()
if __name__ == "__main__":
main()