
- Python - Network Programming
- Python - Network Introduction
- Python - Network Environment
- Python - Internet Protocol
- Python - IP Address
- Python - DNS Lookup
- Python - Routing
- Python - HTTP Requests
- Python - HTTP Response
- Python - HTTP Headers
- Python - Custom HTTP Requests
- Python - Request Status Codes
- Python - HTTP Authentication
- Python - HTTP Data Download
- Python - Connection Re-use
- Python - Network Interface
- Python - Sockets Programming
- Python - HTTP Client
- Python - HTTP Server
- Python - Building URLs
- Python - WebForm Submission
- Python - Databases and SQL
- Python - Telnet
- Python - Email Messages
- Python - SMTP
- Python - POP3
- Python - IMAP
- Python - SSH
- Python - FTP
- Python - SFTP
- Python - Web Servers
- Python - Uploading Data
- Python - Proxy Server
- Python - Directory Listing
- Python - Remote Procedure Call
- Python - RPC JSON Server
- Python - Google Maps
- Python - RSS Feed
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - WebForm Submission
Often the interaction with a webpage needs some data to be submitted to the server through the forms present in the html page. These webforms are typically used for processes like signing up for a new account or supplying some information like name or roll number to retrieve the result of an examination. The requests module handles this gracefully using the POST method with the required parameters.
Example
In the below example we use the sign up form of a website by supplying the userid and password value. After the submission of the values we print the response.
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()
When we run the above program, we get the following output −
Response to GET request: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta HTTP-EQUIV="Pragma" CONTENT="no-cache"> <meta HTTP-EQUIV="Expires" CONTENT="-1"> <title>Login - codepad</title> <link href="/main.css" media="screen" rel="stylesheet" type="text/css" /> <style type="text/css"> </style> <script src='https://www.google.com/recaptcha/api.js'></script> <script> function onRecaptcha(token) { document.getElementById("editor-form").submit(); } </script> </head> <body > ..................... .....................
Advertisements