Skip to content

Instantly share code, notes, and snippets.

@c0ldlimit
Created November 16, 2012 17:15
Show Gist options
  • Save c0ldlimit/4089107 to your computer and use it in GitHub Desktop.
Save c0ldlimit/4089107 to your computer and use it in GitHub Desktop.
Python: Submitting HTTP Requests
# http://www.blog.pythonlibrary.org/2012/06/08/python-101-how-to-submit-a-web-form/
# with urllib
import urllib
import urllib2
import webbrowser
url = "http://duckduckgo.com/html"
data = urllib.urlencode({'q': 'Python'})
results = urllib2.urlopen(url, data)
with open("results.html", "w") as f:
f.write(results.read())
webbrowser.open("results.html")
# with requests
import requests
url = "http://duckduckgo.com/html"
payload = {'q':'python'}
r = requests.post(url, payload)
with open("requests_results.html", "w") as f:
f.write(r.content)
# http://www.voidspace.org.uk/python/articles/urllib2.shtml
# post request
import urllib
import urllib2
url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment