指定出口IP,发起HTTP请求
# -*- coding=utf-8 -*-
import socket
import requests
true_socket = socket.socket
ipbind='xxx.xxx.xxx.xxx'
def bound_socket(*a, **k):
sock = true_socket(*a, **k)
# 端口是0, 操作系统会自动选择可用的端口号
sock.bind((ipbind, 0))
return sock
socket.socket = bound_socket
r = requests.get('http://ip.cn/')
print r.status_code