urllib2 绑定ip来访问url,urllib2绑定ipurl,import urlli
文章由Byrx.net分享于2019-03-23 07:03:29
urllib2 绑定ip来访问url,urllib2绑定ipurl,import urlli
import urllib2, httplib, socketclass BindableHTTPConnection(httplib.HTTPConnection): def connect(self): """Connect to the host and port specified in __init__.""" self.sock = socket.socket() self.sock.bind((self.source_ip, 0)) if isinstance(self.timeout, float): self.sock.settimeout(self.timeout) self.sock.connect((self.host,self.port))def BindableHTTPConnectionFactory(source_ip): def _get(host, port=None, strict=None, timeout=0): bhc=BindableHTTPConnection(host, port=port, strict=strict, timeout=timeout) bhc.source_ip=source_ip return bhc return _getclass BindableHTTPHandler(urllib2.HTTPHandler): def http_open(self, req): return self.do_open(BindableHTTPConnectionFactory('127.0.0.1'), req)opener = urllib2.build_opener(BindableHTTPHandler)opener.open("http://byrx.net/").read() # Will fail, 127.0.0.1 can't reach byrx.net
相关内容
- python设置进程的环境变量,python环境变量,import ospri
- python创建访问对象属性,python创建对象,class Critte
- python NameTag类,pythonnametag类,class NameTa
- python重写父类方法,python重写,class MyList
- python中使用self参数,pythonself参数,class Bag:
- python udp 服务器,pythonudp,import socke
- python虚函数定义,python函数定义,class Specia
- python的get set方法示例,pythongetset示例,class Critte
- 罗马数字,,import strin
- python多父类继承示例,python继承示例,class A:
评论关闭