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 ...
随机推荐
- Adobe Acrobat 9 Pro 注册码
来自百度知道,记录与此,以备后用http://zhidao.baidu.com/question/177914535.html 如果你的系统盘是C盘,那么就删除:c:/Documents and Se ...
- 彻底搞明白find命令的-mtime参数的含义【转载】
转自: 彻底搞明白find命令的-mtime参数的含义-goolen-ITPUB博客http://blog.itpub.net/23249684/viewspace-1156932/ 以前一直没有弄明 ...
- JSP内置对象--application对象(getRealPath(),getAttributeNames(),getContextPath())
application对象是javax.servlet.ServletContext接口的实例化对象.是整个servlet的上下文,代表了整个web容器的操作. 常用方法: 1.java.lang.S ...
- linux dmesg命令
linux dmesg命令详解 功能说明:显示开机信息. 语 法:dmesg [-cn][-s ] 补充说明:kernel会将开机信息存储在ring buffer,若是开机时来不及查看信息,可利用 ...
- linux logrotate配置
对于Linux 的系统安全来说,日志文件是极其重要的工具.系统管理员可以使用logrotate 程序用来管理系统中的最新的事件,对于Linux 的系统安全来说,日志文件是极其重要的工具.系统管理员可以 ...
- CentOS 6.5 安装Nvidia驱动后出现"no screens found "错误
参考官网文档 http://cn.download.nvidia.com/XFree86/Linux-x86_64/280.13/README/editxconfig.html 进入更改文档: vi ...
- over-float清除浮动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 转:使用WITH AS提高性能简化嵌套SQL
使用WITH AS提高性能简化嵌套SQL 一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片 ...
- sitemap制作
sitemap.xml生成方法(asp和php)(转) http://teachmyself.blog.163.com/blog/static/18881422920119895435272/ sit ...
- java多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么?
多线程有两种实现方法,分别是继承Thread类与实现Runnable接口 同步的实现方面有两种,分别是synchronized,wait与notify 先看一下java线程运行时各个阶段的运行状态 j ...