The socket module in Python uses a proxy instance _python

Source: Internet
Author: User
Tags socket in python

Say the socket agent, first of all, to say that the HTTP proxy, Python urllib2 is a self-contained HTTP proxy function, you can use the following code to achieve:

Copy Code code as follows:

Proxy_handler = Urllib2. Proxyhandler ({' http ': ' http://Address: Port '})
Opener = Urllib2.build_opener (Proxy_handler, Urllib2. HttpHandler)
Urllib2.install_opener (opener)

So, the next time you use URLLIB2 to Urlopen, you will automatically use this agent.

However, in our crazy celestial, sometimes, HTTP proxy does not meet the requirements, perhaps you want to use the lower socket proxy, so Python seems to have no direct method (Python3 do not know whether there is no built-in, not research). At this time, you may need this package: Socksipy–a Python SOCKS module, download decompression, in addition to the document also on a socks.py file, very convenient to use, to produce sample code:

Copy Code code as follows:

Import socks, Socket
Socks.setdefaultproxy (socks. PROXY_TYPE_SOCKS5, "Address", Port)
Socket.socket = Socks.socksocket

Next, you can use the socket as usual, everything will be quietly through the agent out of the ~

Related Article

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.