Python requests automatic Cookie processing

Source: Internet
Author: User

In the normal test often encounter the use of Python scripts to simulate the HTTP request operation, in an interface "HTTP request, return JSON" test, the development of the HTTP interface in the request will be blocked by the filter, verify that the requestor's session is legitimate, Since we are directly requesting, without any accompanying cookies and headers, how can we legitimately make an interface request and continue testing? See below for a description:

Before using the system, we will go through a login operation, this login operation will be our SessionID state property set to a legitimate "different system, the details are different, roughly similar", if we want to directly request the interface, and need to bring a legal sessionid in our cookie, how to do it?

There are several ways to do this:

1, manually prepare a legal sessionid, in the request to put into the cookie. Cons: Manual preparation is required, and SessionID will expire.

2, the analog login request, in the response header set cookie will sessionid out, the next request with. Cons: The Set cookie section needs to be processed in the response header, which is cumbersome to handle if there is a large set of cookies.

3, use Cookiejar, simulate the login request, and then simulate the interface request. Advantages: Simple and convenient.

  

  The first method: not introduced here, nor recommended.

The second method: You can refer to the HTTP request when adding the request header.

The third method: see the description below.

Existing interface Queryvideolist:http://192.168.1.242:9000/queryvideolist

Queryvideolist Request parameters: {' catalogsid ': ', ' catagorycode ': ', ' sorttype ': ' desc ', ' SortField ': ' Time ', ' videoinfo ': ', ' BeginTime ': ', ' endTime ': ', ' pageSize ': ' A ', ' pageIndex ': ' 1 '}

Url:http://192.168.1.242:9000/of post at login

What does that mean? You can see through the HTTP Request monitoring tool to monitor the login process. Recommended Fiddler, login will post user name and password, this post URL is the URL of the login, this post operation is mainly to set your sessionid to legal.

Next I'll go directly to the code:

1 #coding:u82 ImportUrllib3 ImportUrllib24 ImportCookielib5 6Cookiejar=Cookielib. Cookiejar ()7Opener=Urllib2.build_opener (urllib2. Httpcookieprocessor (Cookiejar))8 9Login_post_url ="http://192.168.86.242:9000/"TenQuery_video_list_post_url ="http://192.168.1.242:9000/QueryVideoList" One  ALogin_post_data = {'username':'Feixu','Password':'000000','Systype':'1'} -Query_video_list_post_data = {'Catalogsid':"','Catagorycode':"','SortType':'desc','SortField':' Time','Videoinfo':"','BeginTime':"','EndTime':"','pageSize':' -','PageIndex':'1'} -  the #UrlEncode Encoding - defEncode_post_data (post_data): -     returnUrllib.urlencode (post_data) -  + #Post Request general method, use global variable opener, specific cookie collection function - defPost_fun (url,post_data): +     Globalopener Areq=Urllib2. Request (Url,encode_post_data (post_data)) at     returnOpener.open (req) -  - #querying the video list interface - defquery_video_list (url,post_data): -     returnPost_fun (url,post_data) -  in #log in so that the session specified in the cookie becomes legal - defLogin (url,post_data): to     returnPost_fun (url,post_data) +  - Login (login_post_url,login_post_data) theRESP =query_video_list (query_video_list_post_url,query_video_list_post_data) * PrintResp.read ()

First we create a cookiejar, and then use this Cookiejar to build a opener, that is, the late use of opener to make a request, the cookie will be automatically deposited into the Cookiejar container, and then continue the request, Will come with the last cookie that was deposited, so we only need to use opener to make the request, and the cookie is handled automatically by Cookiejar.

What you need to deal with is the URL of the request, the requested data, except the cookie headers request header

Python requests automatic Cookie processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.