Download our e-book of Introduction To Python
Neha Kumawat
3 years ago
import requests
ID_USERNAME = 'signup-user-name'
ID_PASSWORD = 'signup-user-password'
USERNAME = 'username'
PASSWORD = 'yourpassword'
SIGNUP_URL = 'http://codepad.org/login'
def submit_form():
"""Submit a form"""
payload = {ID_USERNAME : USERNAME, ID_PASSWORD : PASSWORD,}
resp = requests.get(SIGNUP_URL)
print "Response to GET request: %s" %resp.content
resp = requests.post(SIGNUP_URL, payload)
print "Headers from a POST request response: %s" %resp.headers
#print "HTML Response: %s" %resp.read()
if __name__ == '__main__':
submit_form()
Response to GET request:
Login - codepad
.....................
.....................
# Python 2.x example
import requests
url = 'https://duckduckgo.com/html/'
payload = {'q':'python'}
r = requests.get(url, params=payload)
with open("requests_results.html", "w") as f:
f.write(r.content)
with open("requests_results.html", "wb") as f:
f.write(r.content)