How to do it...

Let us submit some fake data to register with https://twitter.com/. Each form submission has two methods: GET and POST. The less sensitive data, for example, search queries, are usually submitted by GET and the more sensitive data is sent via the POST method. Let us try submitting data with both of them.

Listing 4.4 explains the submit web forms, as follows:

#!/usr/bin/env python 
# Python Network Programming Cookbook -- Chapter - 4 
# This program requires Python 3.5.2 or any later version 
# It may run on any other version with/without modifications. 
# 
# Follow the comments inline to make it run on Python 2.7.x. 
 
 
import requests 
import urllib 
 
# Uncomment the below line for Python 2.7.x. 
#import urllib2 
 
ID_USERNAME = 'signup-user-name' 
ID_EMAIL = 'signup-user-email' 
ID_PASSWORD = 'signup-user-password' 
USERNAME = 'username' 
EMAIL = '[email protected]' 
PASSWORD = 'yourpassword' 
SIGNUP_URL = 'https://twitter.com/account/create' 
 
 
def submit_form(): 
    """Submit a form""" 
    payload = {ID_USERNAME : USERNAME, 
               ID_EMAIL    :  EMAIL, 
               ID_PASSWORD : PASSWORD,} 
     
    # make a get request 
    resp = requests.get(SIGNUP_URL) 
    print ("Response to GET request: %s" %resp.content) 
     
    # send POST request 
    resp = requests.post(SIGNUP_URL, payload) 
    print ("Headers from a POST request response: %s" %resp.headers) 
 
 
if __name__ == '__main__': 
    submit_form() 

If you run this script, you will see the following output:

$ python 4_4_submit_web_form.py 
Response to GET request: <?xml version="1.0" encoding="UTF-8"?>
    <hash>
      <error>This method requires a POST.</error>
      <request>/account/create</request>
    </hash>
    
Headers from a POST request response: {'status': '200 OK', 'content-
length': '21064', 'set-cookie': '_twitter_sess=BAh7CD--
d2865d40d1365eeb2175559dc5e6b99f64ea39ff; domain=.twitter.com; 
path=/; HttpOnly', 'expires': 'Tue, 31 Mar 1981 05:00:00 GMT', 
'vary': 'Accept-Encoding', 'last-modified': 'Sun, 05 May 2013 
15:59:27 GMT', 'pragma': 'no-cache', 'date': 'Sun, 05 May 2013 
15:59:27 GMT', 'x-xss-protection': '1; mode=block', 'x-transaction': 
'a4b425eda23b5312', 'content-encoding': 'gzip', 'strict-transport-
security': 'max-age=631138519', 'server': 'tfe', 'x-mid': 
'f7cde9a3f3d111310427116adc90bf3e8c95e868', 'x-runtime': '0.09969', 
'etag': '"7af6f92a7f7b4d37a6454caa6094071d"', 'cache-control': 'no-
cache, no-store, must-revalidate, pre-check=0, post-check=0', 'x-
frame-options': 'SAMEORIGIN', 'content-type': 'text/html; 
charset=utf-8'}
  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.55.18