Python fetches data, Python uses SOCKS proxy to fetch data

Source: Internet
Author: User

In Python, the normal fetch data uses the URLLIB2 module directly:

' http://fanyi.baidu.com/ '  == stream.read () Print cont

If we want to go to the HTTP proxy, we can also use URLLIB2, do not need to reference other modules:

' Https://clients5.google.com/pagead/drt/dn/dn.js '  = urllib2. Proxyhandler ({'http':'127.0.0.1:1080' =  = opener.open (URL) print f.read ()

If we want to use the SOCKS5 proxy, we need another module sockes, socket, we can get through the PIP install socksipy, (I am the Windows system, also need another module, pip install Win_inet_pton) It is recommended to download the installation package directly to the Python installation directory, switch to the corresponding directory, execute the python setup.py install installation, is really dead to the wall of the big celestial, and then add the socket code on the module that needs the agent:

" 127.0.0.1 "  the  = Socks.socksocket

The complete code is as follows:

The added code begins with
" 127.0.0.1 " the = socks.socksocket
= Urllib2.urlopen ('https://clients5.google.com/pagead/drt/dn/dn.js') print Steam.read ()

Using the Requests module, we also add the socket code before the requests, and the subsequent HTTP request does not change, or the original logic:

The added code begins with
Import win_inet_ptonimport socketimport socksimport requestsip='localhost' socks.socksocket//= End of code added
' Https://clients5.google.com/pagead/drt/dn/dn.js ' print requests. Get (URL). text

Related:

Github:https://github.com/mitsuhiko/python-geoip/issues/4

NONO
Source: http://www.cnblogs.com/diligenceday/
Enterprise website: http://www.idrwl.com/Xiamen Lighting the Future network technology
Open Source Blog: Http://www.github.com/sqqihao
QQ: 287101329
: 18101055830

Xiamen Lighting Future Network Technology Co., Ltd., is the best application of Xiamen, small program, website, the public number development company

Python fetches data, Python uses SOCKS proxy to fetch data

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.