在做采集器的过程中,经常会遇到IP限制的情况,这时候可以通过切换IP能继续访问。

如果是多IP的服务器,那么可以通过切换出口Ip来实现。

1.首先是如何获取服务器绑定的IP

import netifaces as ni
def getLocalEthIps():
for dev in ni.interfaces():
if dev.startswith('eth0'):
ip=ni.ifaddresses(dev)[2][0]['addr']
if ip not in ipList:
ipList.append(ip) print getLocalEthIps()

需要引入netifaces模块,安装方法easy_install netifaces

2.为socket绑定出口IP

# -*- coding=utf-8 -*-
import socket
import urllib2
import re
true_socket = socket.socket ipbind='xx.xx.xxx.xx' def bound_socket(*a, **k):
sock = true_socket(*a, **k)
sock.bind((ipbind, 0))
return sock socket.socket = bound_socket response = urllib2.urlopen('http://www.ip.cn')
html = response.read()
ip=re.search(r'code.(.*?)..code',html)
print ip.group(1)

后面这个是通过访问ip.cn来验证实际外网IP, 正则原来是r'<code>(.*?)</code>'的,由于博客的代码冲突,所以就改了下。

参考自:http://stackoverflow.com/questions/1150332/source-interface-with-python-and-urllib2

3.我实现的随机绑定出口IP

#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
import random
import netifaces as ni true_socket = socket.socket
ipList=[] class getLocalIps():
global ipList
def getLocalEthIps(self):
for dev in ni.interfaces():
if dev.startswith('eth0'):
ip=ni.ifaddresses(dev)[2][0]['addr']
if ip not in ipList:
ipList.append(ip) class bindIp():
ip=''
global true_socket,ipList def bound_socket(self,*a, **k):
sock = true_socket(*a, **k)
sock.bind((self.ip, 0))
return sock def changeIp(self,ipaddress):
self.ip=ipaddress
if not self.ip=='':
socket.socket = self.bound_socket
else:
socket.socket = true_socket def randomIp(self):
if len(ipList)==0:
getLocalIpsFunction=getLocalIps()
getLocalIpsFunction.getLocalEthIps()
if len(ipList)==0:
return
_ip=random.choice(ipList)
if not _ip==self.ip:
self.changeIp(_ip) def getIp(self):
return self.ip def getIpsCount(self):
return len(ipList)

4.调用示例

bindIpObj= bindIp()
#随机切换IP
bindIpObj. randomIp()
#得到当前IP
print bindIpObj.getIp()
#得到本机IP数
print bindIpObj.getIpsCount()
#切换到指定IP
print bindIpObj. changeIp('xxx.xx.xx.xxx')

python获取绑定的IP,并动态指定出口IP的更多相关文章

  1. 多IP指定出口IP地址 如何指定云服务器源IP?

    如果一个主机绑定有多个IP地址,那么在被动响应和主动发起连接两种方式中,源IP地址的选择机制肯定是有所差异的.主机在接收外部数据包,并发送响应数据包时,响应源地址显然就是客户端请求的地址,这是非常容易 ...

  2. java网络访问指定出口ip

    java网络访问指定出口ip Table of Contents 1. socket 2. apache httpclient 1 socket 可以在Socket构造函数中指定使用的本地ip,如: ...

  3. 突破IP限制动态替换代理ip。

    须要导入的两个jar包 实现的javabean <span style="font-size:18px;">package com.jx.po; public clas ...

  4. Python 获取Google+特定用户最新动态

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-28 @author: guaguastd @name: l ...

  5. python 获取探针页面,自动查询公司出口

    在一些渗透当中,我们需要批量探针出口来达到我们的目的. 所以就有了这个丑陋简洁的小脚本. #!/usr/bin/env python #-*- coding:utf- -*- import sys i ...

  6. 记录阿里云安全组设置遇到的奇葩问题--出口ip

    之前公司使用的路由器里使用的是PPPOE拨号的形式上网的,根据拨号后得到的ip地址,配置到阿里云的安全组里,具体来说是配置到22端口里,也就是说只有特定ip才能访问22端口,也即是说只允许公司网络远程 ...

  7. Python获取本机的mac,ip,name

    Python获取mac 获取计算机名字和ip(内网ip) 指定网卡ip

  8. 在.bashrc中,使用python获取本机IP地址(现在只支持wlan)

    其实最好的办法是写个单独的脚本去查找IP,但是如果实在不愿意单写一个脚本文件,也可以直接将代码嵌入.bashrc中 在~/.bashrc下加入下面这行代码即可使用python获取本机的wlan的IP地 ...

  9. python获取本机IP、mac地址、计算机名

    在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: 1 2 3 4 import uuid de ...

随机推荐

  1. 禁止root远程登录

    Linux禁止root远程登录 ssh的修改vi /etc/ssh/sshd_config将默认的#PermitRootLogin yes去注释改为PermitRootLogin no service ...

  2. linux性能优化cpu 磁盘IO MEM

    系统优化是一项复杂.繁琐.长期的工作,优化前需要监测.采集.测试.评估,优化后也需要测试.采集.评估.监测,而且是一个长期和持续的过程,不 是说现在优化了,测试了,以后就可以一劳永逸了,也不是说书本上 ...

  3. emacs redo

    c / c/ cg c/ cg c/ tramp: /sudo::/usr/

  4. 硬盘安装Win7、CentOS7双系统

    待补充 0.软件 Acronis Disk Director:用来对硬盘分区,将磁盘的一部分格式化成Linux可以识别的ext3格式 Ext2Fsd:因为Windows不能识别ext3格式的文件系统, ...

  5. 打包Egret游戏为Chrome extension

    今天,本来是打算做一个Chrome扩展去爬取网站base64编码图片的. 在跟着图灵社区<Chrome扩展及应用开发>敲demo代码的过程中,发现chrome的扩展的结构理论上可以兼容所有 ...

  6. Activity进程和线程之间的关系

    1,四大组件并不是程序(进程)的全部,只是他的零件. 2,应用程序启动后,将创建ActivityThread主线程. 3,同一包中的组件将运行在想通的进程空间里面. 4,不同包中的组件可以通过一定的方 ...

  7. IMCP网际控制协议

    IP协议是TCP/IP协议使用的在网络层传输机制,它是一种不可靠的无连接的数据报协议,但是IP协议假定了底层是不可靠的,因此,要尽最大的努力传输到目的地,但正因为如此,IP协议则没有了保证,也就是说, ...

  8. mysql时间函数,总是记不住,总是查。

    http://www.cnblogs.com/zeroone/archive/2010/05/05/1727659.html UNIX_TIMESTAMP() UNIX_TIMESTAMP(date) ...

  9. PAT (Advanced Level) 1082. Read Number in Chinese (25)

    模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  10. CodeForces 614D Skills

    排序+枚举+二分 最大的那些变成A,小的那部分提高最小值 #include<cstdio> #include<cstring> #include<cmath> #i ...