推荐学习书目
› 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
yuexiande
V2EX  ›  Python

如何实现 request 指定使用出口 ip

  •  
  •   yuexiande · Mar 27, 2016 · 3614 views
    This topic created in 3871 days ago, the information mentioned may be changed or developed.
    如何实现 request 指定使用出口 ip ,我的服务器网卡有多个 ip ,
    默认是一个地址,但是如何使用其他的网卡 ip , request 访问网站
    2 replies  •  2016-04-05 18:29:56 +08:00
    setiepr
        2
    setiepr  
       Apr 5, 2016
    import socket

    real_create_conn = socket.create_connection

    def set_src_addr(*args):
    address, timeout = args[0], args[1]
    source_address = ('IP_ADDR_TO_BIND_TO', 0)
    return real_create_conn(address, timeout, source_address)

    socket.create_connection = set_src_addr

    import requests
    r = requests.get('http://www.google.com')
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1217 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:20 · PVG 06:20 · LAX 14:20 · JFK 17:20
    ♥ Do have faith in what you're doing.