Skip to content

Instantly share code, notes, and snippets.

@lerouxb
Created November 29, 2010 15:05
Show Gist options
  • Select an option

  • Save lerouxb/720047 to your computer and use it in GitHub Desktop.

Select an option

Save lerouxb/720047 to your computer and use it in GitHub Desktop.
Do a POST request on a URL, print status, headers and response body.
#!/usr/bin/env python
import httplib
import sys
from urlparse import urlparse
def post(url, params):
r = urlparse(url)
conn = httplib.HTTPConnection(r.netloc)
pathquery = r.path
if r.query:
pathquery = pathquery + '?' + r.query
conn.request("POST", pathquery, params)
res = conn.getresponse()
print res.status, res.reason
for k, v in res.getheaders():
print k+': '+v
print
print res.read()
conn.close()
if __name__ == "__main__":
if len(sys.argv) >= 2:
url = sys.argv[1]
params = ''
if len(sys.argv) > 2:
params = sys.argv[2]
post(url, params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment