Last active
August 29, 2015 14:14
-
-
Save realduke2000/14e05bac2f6977903bc9 to your computer and use it in GitHub Desktop.
quick python http proxy
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
#!/usr/bin/python | |
max_data_len = 1024000 | |
import socket, sys | |
def main(argv): | |
if len(argv) < 3: | |
usage() | |
return | |
lp = int(argv[0]) | |
rip = argv[1] | |
rp = int(argv[2]) | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server_address = ('0.0.0.0', lp) | |
sock.bind(server_address) | |
sock.listen(1) | |
while True: | |
connection, client_address = sock.accept() | |
data = connection.recv(max_data_len) | |
if data: | |
# forward to server | |
resp = forward(rip, rp, data) | |
connection.sendall(resp) | |
connection.close() | |
def forward(ip, port, data): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server_address = (ip, port) | |
sock.connect(server_address) | |
sock.sendall(data) | |
ret_data = sock.recv(max_data_len) | |
sock.close() | |
return ret_data | |
def usage(): | |
print("usage:") | |
print("python " + __file__ + " [local port] [remote ip] [remote port]") | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment