推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
kojp
V2EX  ›  Python

python 使用代理

  •  
  •   kojp · Mar 16, 2012 · 7989 views
    This topic created in 5316 days ago, the information mentioned may be changed or developed.
    是这样的,以前有写过一个,下载CSS文件背景图片的小工具。

    国内的网站都算是畅通无阻。

    但是对于被墙的就不行了。

    想在urllib.urlretrieve 之前加一个代理?要怎么操作。。。

    而且加的代理是自己的SSH~~~是否能实现?
    7 replies  •  1970-01-01 08:00:00 +08:00
    kojp
        1
    kojp  
    OP
       Mar 16, 2012
    proxies = {'http': '127.0.0.1:8282'}
    urllib.urlretrieve(url(....), proxies=proxies)
    gavingeng
        2
    gavingeng  
       Mar 19, 2012
    如果是使用ssh的话,可以使用socks.py来做设置
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 7070)
    socket.socket = socks.socksocket

    你需要到SocksiPy.zip的包,然后cp到你的py lib下就可以
    kojp
        3
    kojp  
    OP
       Mar 20, 2012
    @gavingeng
    现在试试。。。一会儿,反馈。呵呵。

    谢谢~
    kojp
        4
    kojp  
    OP
       Mar 22, 2012
    @gavingeng

    亲测,有用。。(大部分网站有用。及少部分表示超时,原因不明)

    import urllib2,io,os,sys,socks,socket
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 8282)
    socket.socket = socks.socksocket
    #req=urllib2.Request("http://www.twitter.com") #ok
    req=urllib2.Request("https://www.facebook.com/") #not ok
    f=urllib2.urlopen(req)
    s=f.read()
    print s
    yanwen
        5
    yanwen  
       Mar 22, 2012
    php + curl 好像也可以实现这样的功能
    gavingeng
        6
    gavingeng  
       Mar 22, 2012
    @kojp 嘿嘿!
    est
        7
    est  
       Mar 22, 2012
    直接环境变量 export http_proxy=x.x.x.x:8000
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   851 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 18:49 · PVG 02:49 · LAX 11:49 · JFK 14:49
    ♥ Do have faith in what you're doing.