Skip to content

Instantly share code, notes, and snippets.

@andybp85
Last active August 29, 2015 14:21
Show Gist options
  • Save andybp85/68a91a6be7e723d2b4df to your computer and use it in GitHub Desktop.
Save andybp85/68a91a6be7e723d2b4df to your computer and use it in GitHub Desktop.
Simple Python HTTP POST Redirector
"""Simple Python HTTP POST Redirector - Redirects POST requests with data.
Author: Andrew Stanish, May 2015
Uncomment the two pprint lines to write the POST data to the console.
Change the address in this line to send wherever you like:
requests.post('http://10.8.0.10:8080', data=form.value, headers=headers )
"""
import cgi, requests, BaseHTTPServer
#import pprint as pp
class HandlerClass(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(s):
s.send_response(200)
s.end_headers()
form = cgi.FieldStorage(
fp = s.rfile,
headers = s.headers,
environ = {'REQUEST_METHOD':'POST',
'CONTENT_TYPE':s.headers['Content-Type']})
headers = {'Content-Type' : s.headers['Content-Type'],
'User-Agent' : s.headers['User-Agent']}
#pp.pprint(form.value)
requests.post('http://10.8.0.10:8080', data=form.value, headers=headers )
if __name__ == "__main__":
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
port = 8080
server_address = ('', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
# print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment