среда, 17 июля 2013 г.

Python: set network interface for urllib2

The problem is that urllib2 is based on httplib library which doesn't have such functionality. The best solution I found was this “monkey patch” from Alex Martelli posted on Stack Overflow:
import socket
true_socket = socket.socket
def bound_socket(*a, **k):
    sock = true_socket(*a, **k)
    sock.bind((sourceIP, 0))
    return sock
socket.socket = bound_socket
Simply run this block of code before using urllib2 functions.

Комментариев нет:

Отправить комментарий