I found a script on this site for running a simple server via the command line with python.
I added some print
lines in because I\'d like to print out the G
You can use cgi
module instead of urlparse
. cgi
implements POST params parsing out of the box. Using well-tested libraries seems better.
import cgi
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={"REQUEST_METHOD": "POST"}
)
for item in form.list:
print "%s=%s" % (item.name, item.value)
It's not tremendously obvious, but the handler is using sockets behind the scenes. So you need to read the raw data from the socket, and then interpret it.
Use the urlparse module.
urlparse.parse_qs
.urllib.parse.parse_qs
.Import urlparse
, and then modify your do_POST
method like so:
def do_POST(s):
"""Respond to a POST request."""
# Extract and print the contents of the POST
length = int(s.headers['Content-Length'])
post_data = urlparse.parse_qs(s.rfile.read(length).decode('utf-8'))
for key, value in post_data.iteritems():
print "%s=%s" % (key, value)
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
...
Set up a simple test client:
#!/usr/bin/env python
import urllib
import urllib2
url = 'http://localhost:9000'
post_dict = {'foo' : 1,
'bar' : 2,
'bis' : 3}
params = urllib.urlencode(post_dict)
post_req = urllib2.Request(url)
post_req.add_data(params)
response = urllib2.urlopen(post_req)
response_data = response.read()
response.close()
print response_data
Start the server, and then run the client:
ire@localhost$ python http_server.py
Wed Oct 3 21:38:51 2012 Server Starts - localhost:9000
foo=[u'1']
bar=[u'2']
bis=[u'3']