Python Forum

Full Version: How to intercept and modify form POST values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a simple form on a remote webserver:

<form action="post.php" method="post">
    <input type="text" name="p" value="abc">
    <input type="submit">
</form>
I load the remote page on my local browser by calling the url as usual.

The target file post.php simply print the value of the POST param:

Output:
Array ( [p] => abc )


I need a Python code that:
  1. Runs on my local computer (the same one where the browser runs),
  2. Intercept the POST sent by the browser, modify the param p value (let's say xyz), and lastly
  3. Send the results to the original remote post.php, so that it will print back:
    Output:
    Array ( [p] => xyz )

I think it can be done by a Python proxy that acts as mitm (even using libraries, may be mitmproxy?).
I'll configure my browser to point to this local python proxy.
I am a newbie and don't know how to start: a simple code will help me to understand.

Thank you in advance for your time,
Gilberto
I guess the first step is getting a proxy server that just works, and after that try modifying post data. To that end, here's a pretty simple example: http://effbot.org/librarybook/simplehttpserver.htm
Quote:
# a truly minimal HTTP proxy

import SocketServer
import SimpleHTTPServer
import urllib

PORT = 1234

class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.copyfile(urllib.urlopen(self.path), self.wfile)

httpd = SocketServer.ForkingTCPServer(('', PORT), Proxy)
print "serving at port", PORT
httpd.serve_forever()
Thank you @nilamo, I'm trying the code right now.

One note: the code is not for Python 3.6.5 (I have this version).

SocketServer becomes socketserver

I get the error:
Error:
ModuleNotFoundError: No module named 'SimpleHTTPServer'
I don't know how to adapt the code to v. 3.6.5

Any help?
Thank you. I'll study the doc.