User-Profile-Image
hankin
  • 5
  • 首页
  • 分类
    • Windows
    • Web
    • VoIP
    • Virtualization
    • Trading
    • Smart Home
    • SEO
    • Security
    • Python
    • Program
    • PHP
    • OSX
    • OS
    • OpenSource
    • Nginx
    • MySQL
    • Mood
    • Linux
    • Life
    • Injection
    • Hosting
    • Finance
    • Exploits
    • CDN
  • 页面
    • About
  • 友链
    • 维酷科技
    • Tesra超算网络
Help?

Please contact us on our email for need any support

Support
  • 首页
    首页   ›   Program   ›   Python   ›   正文
Python

用Python指定HTTPConnection的出口IP(Specify outgoing ip)

2010-09-18 18:48:11
2755  0 0
问题描述:
客户端设置了多IP,想让不同的python脚本使用不同的IP访问远程服务器,怎么办?或者说如何用python指定出口IP(specify outgoing ip)?

解决:
真是花了我不少力气,被折腾死之后,发现python的socket是支持ip绑定的,用socket.bind就行了

再参考了这贴 http://bugs.python.org/issue3972

我写了个HTTPConnection_with_ip_binding的类,继承了httplib.HTTPConnection,修改了初始化,和conn ect函数,加了个参数bindip,试验成功,代码如下

import httplib 
import socket 
class HTTPConnection_with_ip_binding(httplib.HTTPConnection): 
    def __init__(self, host, port=None, strict=None, 
                timeout=socket._GLOBAL_DEFAULT_TIMEOUT, bindip=None): 
        httplib.HTTPConnection.__init__(self,host,port,strict,timeout) 
        self.bindip = bindip 
    def connect(self): 
        msg = “getaddrinfo returns an empty list” 
        for res in socket.getaddrinfo(self.host, self.port, 0, 
                                      socket.SOCK_STREAM): 
            af, socktype, proto, canonname, sa = res 
            try: 
                self.sock = socket.socket(af, socktype, proto) 
                if self.debuglevel > 0: 
                    print “connect: (%s, %s)” % (self.host, self.port) 
                if self.bindip is not None: 
                    self.sock.bind((self.bindip,0)) 
                self.sock.connect(sa) 
            except socket.error, msg: 
                if self.debuglevel > 0: 
                    print ‘connect fail:’, (self.host, self.port) 
                if self.sock: 
                    self.sock.close() 
                self.sock = None 
                continue 
            break 
        if not self.sock: 
            raise socket.error, msg 
def testip(): 
    conn = HTTPConnection_with_ip_binding 
(host=”darwin.bio.uci.edu”,bindip=”64.13.223.117″) 
    conn.request(“GET”, “/cgi-bin/ipecho.pl”) 
    r1 = conn.getresponse() 
    print r1.status, r1.reason 
    data1 = r1.read() 
    print data1 
    conn.close() 
if __name__==’__main__’: 
    testip()执行发现返回了正确的ip

200 OK 
<HTML><HEAD> 
<TITLE>IP Echo</TITLE> 
</HEAD> 
<BODY><H2>Your Computer’s IP Number</H2><dl> 
<dt><b>REMOTE_ADDR</b> 
<dd><i>64.13.223.117</i> 
<dt><b>REMOTE_HOST</b> 
<dd><i></i> 
<dt><b>REMOTE_PORT</b> 
<dd><i>36112</i> 
</dl> 
</body></html>

评论 (0)

Click here to cancel reply.

欢迎您 游客  

    ProdigYu
    180文章 77评论 4点赞 554021浏览
    随机文章
    CentOS7安装Gnome桌面及VNC远程方便挂机
    9年前
    20款开源的搜索引擎系统
    15年前
    Python+Memcached网页优化
    15年前
    Linux下Screen命令详解
    15年前
    Discuz! 7.1 & 7.2 远程代码执行漏洞
    15年前
    Tags
    Apache CentOS CMD DDOS Debian DNS Docker FastCGI Google IIS IP iptables Linux Mac MySQL Nginx Opensource PHP Python Skill SSH Ubuntu Vim VMware VPN web.py Windows 删除 命令 命令提示符 安装 开源 技巧 插件 漏洞 百度 系统 组件 终端 缓存 网络电话 脚本 自建 详解 错误
    Meta
    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    Copyright © 2025
    主页
    页面
    • About
    博主
    ProdigYu 管理员
    Something about myself
    180 文章 77 评论 554021 浏览
    测试
    测试