Make your own active test with Python--self-motivated test on server side (3)-many other HTTP client instances

Source: Internet
Author: User
Tags keep alive

In the previous article just implemented a very easy HTTP client feature, request also provides keep alive, SSL, multi-file upload, cookie management function, HTTP requests header management and other rich features, just to your browser to implement the function, Requests inside all support.

#!/usr/bin/env python#coding=utf-8import requestsdef login_douban (username, passwd): post_data={' source ': ' Index_ Nav ', ' form_email ': username, ' Form_password ':p asswd} request_headers={"User-agent": "mozilla/5.0 (Windows NT 6.1; rv:30.0) gecko/20100101 firefox/30.0 "} response=requests.post (" Http://www.douban.com/accounts/login ", Data=post_ Data,headers=request_headers) if u "Little Prince" in Response.text:print "Login successful" return response El Se:print "Login failed" Print Response.text return falsedef say_something (Login_cookie): Post_da ta={' ck ': ' YNNL ', ' rev_title ': U ' hair welfare ', ' rev_text ': U ' landlord is the title party ', ' rev_submit ': U ' OK, speak '} response=requests.post ("http:// Www.douban.com/group/beijing/new_topic ", Data=post_data,cookies=login_cookie) if response.url=="/http www.douban.com/group/beijing/": print" post new content successfully "return True Else:print" Po St Content fail "return Falselogin_response=login_douban (Your_uSENAME,YOUR_PASSWD) say_something (login_response.cookies) 

request_headers={"user-agent": "mozilla/5.0 (Windows NT 6.1; rv:30.0) gecko/20100101 firefox/30.0 "}, the purpose of this line is to simulate this request is Firefox sent out, very many sites in order to block the crawler, will be user-agent this field to block, Of course today's large sites should be more advanced means to screen the crawler, assuming not set this user-agent, requests sent to the request, User-agent value is python-requests/2.3.0 cpython/2.7.3 windows/7\r\n.


Say_something This function did not test, just I frequently debugging, watercress to I enter the login verification code, there is a question here, I have a period of time in the debugging.


With respect to cookies, the session manages this piece, assuming that it is within the same function that the request has actively managed the session without the need for additional processing,

Session = requests. Session () session.post ("Http://www.douban.com/accounts/login", Data=post_data,headers=request_headers) Session.post ("Http://www.douban.com/group/beijing/new_topic", Data=post_data)

This makes it possible to post successfully.


See here everyone will think selenium, is not the same as requests? Requests is more adept at testing without UI interfaces, and selenium is better at web testing with UI.

Make your own active test with Python--self-motivated test on server side (3)-many other HTTP client instances

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.