Using proxies when impersonating HTTP requests in Python

Source: Internet
Author: User

In some cases it is necessary for us to set the proxy server when we request, under what circumstances we decide, then how to set the HTTP request agent?

Like Python automatically handles cookies, use urllib2. Build_opener to build.

1 ImportUrllib22 3Enable_proxy =True4Proxy_handler = Urllib2. Proxyhandler ({"http":'http://192.168.1.235:8080'})5Null_proxy_handler =Urllib2. Proxyhandler ({})6 7 ifEnable_proxy:8Opener =Urllib2.build_opener (Proxy_handler)9 Else:TenOpener =Urllib2.build_opener (Null_proxy_handler) One  AUrllib2.install_opener (opener)

Using Urllib2.install_opener () sets the global opener of the Urllib2 , which is handy for later use .

1 req=urllib2. Request (Url,encode_post_data (post_data))2 opener.open (req)

Using proxies when impersonating HTTP requests in Python

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.