Python程序中添加代理信息示例

requests

        #! -*- encoding:utf-8 -*-
        import requests
        import random
        # 要访问的目标页面
        targetUrl = "http://httpbin.org/ip"
        # 要访问的目标HTTPS页面
        # targetUrl = "https://httpbin.org/ip"
        # 代理服务器
        proxyHost = "t.16yun.cn"
        proxyPort = "31111"
        # 代理隧道验证信息
        proxyUser = "username"
        proxyPass = "password"
        proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
            "host" : proxyHost,
            "port" : proxyPort,
            "user" : proxyUser,
            "pass" : proxyPass,
        }
        # 设置 http和https访问都是用HTTP代理
        proxies = {
            "http"  : proxyMeta,
            "https" : proxyMeta,
        }
        #  设置IP切换头
        tunnel = random.randint(1,10000)
        headers = {"Proxy-Tunnel": str(tunnel)}
        resp = requests.get(targetUrl, proxies=proxies, headers=headers)
        print resp.status_code
        print resp.text

    原文作者:mmz_77
    原文地址: http://blog.itpub.net/31522063/viewspace-2638989/
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞