python获取绑定的IP,并动态指定出口IP
在做采集器的过程中,经常会遇到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的更多相关文章
- 多IP指定出口IP地址 如何指定云服务器源IP?
如果一个主机绑定有多个IP地址,那么在被动响应和主动发起连接两种方式中,源IP地址的选择机制肯定是有所差异的.主机在接收外部数据包,并发送响应数据包时,响应源地址显然就是客户端请求的地址,这是非常容易 ...
- java网络访问指定出口ip
java网络访问指定出口ip Table of Contents 1. socket 2. apache httpclient 1 socket 可以在Socket构造函数中指定使用的本地ip,如: ...
- 突破IP限制动态替换代理ip。
须要导入的两个jar包 实现的javabean <span style="font-size:18px;">package com.jx.po; public clas ...
- Python 获取Google+特定用户最新动态
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-28 @author: guaguastd @name: l ...
- python 获取探针页面,自动查询公司出口
在一些渗透当中,我们需要批量探针出口来达到我们的目的. 所以就有了这个丑陋简洁的小脚本. #!/usr/bin/env python #-*- coding:utf- -*- import sys i ...
- 记录阿里云安全组设置遇到的奇葩问题--出口ip
之前公司使用的路由器里使用的是PPPOE拨号的形式上网的,根据拨号后得到的ip地址,配置到阿里云的安全组里,具体来说是配置到22端口里,也就是说只有特定ip才能访问22端口,也即是说只允许公司网络远程 ...
- Python获取本机的mac,ip,name
Python获取mac 获取计算机名字和ip(内网ip) 指定网卡ip
- 在.bashrc中,使用python获取本机IP地址(现在只支持wlan)
其实最好的办法是写个单独的脚本去查找IP,但是如果实在不愿意单写一个脚本文件,也可以直接将代码嵌入.bashrc中 在~/.bashrc下加入下面这行代码即可使用python获取本机的wlan的IP地 ...
- python获取本机IP、mac地址、计算机名
在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: 1 2 3 4 import uuid de ...
随机推荐
- AngularJS Front-End App with Cloud Storage Tutorial Part 1: Building a Minimal App in Seven Steps
原文 : http://www.codeproject.com/Articles/1027709/AngularJS-Front-End-App-with-Cloud-Storage-Tutoria ...
- 强制设置IE浏览器的版本模式
转载自:http://blog.csdn.net/huwenhu2007/article/details/17954119 1.<meta http-equiv="X-UA-Compa ...
- Android Studio调试功能使用总结---转
Android Studio调试功能使用总结[转] 这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码 ...
- C/C++ strtok函数
一.头文件:#include <string.h> 二.函数原型:char * strtok (char *str, const char * delimiters); 三.参数:str, ...
- PKI 笔记
PKI – Public Key Infrastructure , 通常翻译为公钥基础设施. PKI 安全平台提供的4个服务,来保证安全的数据,分别是: l 身份识别 l 数据保密 l 数据完整 ...
- Hadoop集群搭建的密钥配置SSH实现机制
- phpstorm9如何配置interpreter
找到php.exe的路径. 把php.exe 放进去就ok了
- 关于Bean\Entity\Model\POJO的一些个人理解
本文没有长篇累牍的,严格的,标准的表述,只是我在开发过程中,读书过程中的一些个人理解,可能不太准备,但是我觉得应该是最方便初学者理解的吧? 一.Bean 对于Bean而言,我的理解是只要是Java的类 ...
- 注册表检测office版本
#region 查询注册表,判断本机是否安装Office2003,2007和WPS public int ExistsRegedit() { int ifused = 0; RegistryKey r ...
- webapp之路--apple私有属性apple-touch-icon
以前我们用过favicon在浏览器给网站进行身份标识,用法如下: <link href="http://image.feeliu.com/web/favicon.ico" r ...