V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
qazwsxkevin
V2EX  ›  Python

求教 Python 调试技巧,能否有简单的方式,决定 code 使用 proxy 代理服务器?

  •  
  •   qazwsxkevin · Feb 20, 2020 · 1104 views
    This topic created in 2261 days ago, the information mentioned may be changed or developed.
    写 code 的场景有两个地方,A side 和 B side
    A 的互联网线路没有做 fliter rule.
    B 的互联网线路做了 fliter rule.

    code 是正常的,
    B side 有些麻烦,在 B side 运行起来后经常触发 rule 导致 request 被 block.
    为此,我做了一个可用的 proxy (sock5 类型)
    为了做好调试阶段的事情,我需要在 B side 里使 code 的全部 request 动作都使用这个 proxy
    把 laptop 带到 A side,是用不了这个 proxy 的

    多写两三句环境判断不是问题
    问题是,我总不能每次写 request,都要做这种特殊场景的判断,啰嗦又影响性能
    是否有一种好办法,弄一下,pycharm 里调试的代码全部都走 proxy 呢?

    by the way:发现 pycharm 里面的 proxy 设置,不是给 code 用的,是给 pip 服务以及 pycharm 软件本身用的,不知道这个判断有无错?
    7 replies    2020-02-21 01:28:10 +08:00
    NeezerGu
        1
    NeezerGu  
       Feb 20, 2020
    非专业人士口胡:
    如果是我的话,
    开场一个全局变量 server=1 or 0 根据丢在哪个服务器手动改

    然后整个装饰器,比如:
    def chose(f):
    def wapper(*args,**kwargs):
    global server,proxy
    if server=1:
    return f(proxy=proxy,*args,**kwargs)
    else:
    return f(*args,**kwargs)
    return wapper

    最后这样(因为我通常只用 get 和 post……,headers 和 cookies 都是手动处理,,没用过 session,可能显得很随意+不专业)
    class s:
    @staticmethod
    @retry
    def get(*args,**kwargs):
    return requests.get(*args,**kwargs)

    @staticmethod
    @retry
    def post(*args,**kwargs):
    return requests.post(*args,**kwargs)
    NeezerGu
        2
    NeezerGu  
       Feb 20, 2020
    艹,辛辛苦苦拍了半天空格,丢进来格式全无
    ![image.png]( https://i.loli.net/2020/02/20/VZFXb54IyaeJPUu.png)
    NeezerGu
        3
    NeezerGu  
       Feb 20, 2020
    二连艹,经常写 sql,做 excel,if server=1 就请假装没看到吧
    scukmh
        4
    scukmh  
       Feb 20, 2020
    cz5424
        5
    cz5424  
       Feb 20, 2020 via iPhone
    弄个环境变量,有的话自动加载就好了
    AlexaZhou
        6
    AlexaZhou  
       Feb 20, 2020 via iPhone
    通过 proxychain 运行 B side 的代码即可
    wzwwzw
        7
    wzwwzw  
       Feb 21, 2020
    读系统变量,来判断当前环境。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2669 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 11:21 · PVG 19:21 · LAX 04:21 · JFK 07:21
    ♥ Do have faith in what you're doing.