V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2012-03-16 18:10:40 +08:00 · 6852 次点击
    这是一个创建于 4424 天前的主题,其中的信息可能已经有所发展或是发生改变。
    是这样的,以前有写过一个,下载CSS文件背景图片的小工具。

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

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

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

    而且加的代理是自己的SSH~~~是否能实现?
    7 条回复    1970-01-01 08:00:00 +08:00
    kojp
        1
    kojp  
    OP
       2012-03-16 18:13:44 +08:00
    proxies = {'http': '127.0.0.1:8282'}
    urllib.urlretrieve(url(....), proxies=proxies)
    gavingeng
        2
    gavingeng  
       2012-03-19 00:31:53 +08:00
    如果是使用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
       2012-03-20 12:40:14 +08:00
    @gavingeng
    现在试试。。。一会儿,反馈。呵呵。

    谢谢~
    kojp
        4
    kojp  
    OP
       2012-03-22 15:46:00 +08:00
    @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  
       2012-03-22 15:51:29 +08:00
    php + curl 好像也可以实现这样的功能
    gavingeng
        6
    gavingeng  
       2012-03-22 22:34:50 +08:00
    @kojp 嘿嘿!
    est
        7
    est  
       2012-03-22 23:01:43 +08:00
    直接环境变量 export http_proxy=x.x.x.x:8000
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1034 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 19:36 · PVG 03:36 · LAX 12:36 · JFK 15:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.