Skip to content

Instantly share code, notes, and snippets.

@fireball2018
Created April 11, 2013 09:34
Show Gist options
  • Select an option

  • Save fireball2018/5362010 to your computer and use it in GitHub Desktop.

Select an option

Save fireball2018/5362010 to your computer and use it in GitHub Desktop.
Python requests 绑定源IP
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment