Fork me on GitHub

# -*- 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

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注