Created
July 11, 2020 09:02
-
-
Save codemicro/e38c10165e31649428291e23f43143fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Modified version of https://github.com/techalchemy/python-proxyserver/blob/master/proxyserver.py | |
# Set these to control bind host and port for tcp server | |
# Port/host that you will connect to with your client | |
BIND_HOST, BIND_PORT = "localhost", 9999 | |
# Set these to control where you are connecting to | |
# Server to send proxied traffic to | |
HOST, PORT = "www.example.com", 80 | |
from socketserver import BaseRequestHandler, TCPServer | |
from socket import socket, AF_INET, SOCK_STREAM | |
class SockHandler(BaseRequestHandler): | |
""" | |
Request Handler for the proxy server. | |
Instantiated once time for each connection, and must | |
override the handle() method for client communication. | |
""" | |
def handle(self): | |
# self.request is the TCP socket connected to the client | |
self.data = self.request.recv(1024) | |
print("Passing data from: {}".format(self.client_address[0])) | |
print(self.data) | |
# Create a socket to the localhost server | |
sock = socket(AF_INET, SOCK_STREAM) | |
# Try to connect to the server and send data | |
try: | |
sock.connect((HOST, PORT)) | |
sock.sendall(self.data) | |
# Receive data from the server | |
while 1: | |
received = sock.recv(1024) | |
if not received: break | |
# Send back received data | |
self.request.sendall(received) | |
finally: | |
sock.close() | |
if __name__ == '__main__': | |
# Create server and bind to set ip | |
myserver = TCPServer((BIND_HOST, BIND_PORT), SockHandler) | |
# activate the server until it is interrupted with ctrl+c | |
myserver.serve_forever() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment