python脚本 - 迁
正文
do_curl.py
用于设置dns域名服务器和设置dns缓存
# -*- coding: utf-8 -*- import os,sys import time import sys import pycurl import re import pprint import StringIO import socket import uuid import urllib import dns from dns import resolver class _SetDNS(object): """ 设置dns缓存,指定nameserver """ _dnscache = {} def __init__(self, nameservers=['114.114.114.114'], is_cached=False): super(_SetDNS, self).__init__() self.nameservers = nameservers # self.domainName = domainName self.is_cached = is_cached self.timeout = 1 self.is_cached = is_cached def set_cached(self): if self.domainName in self._dnscache: # print self.domainName+"in cache" return self._dnscache[self.domainName] else: # print self.domainName+" not in cache" return def resolver(self,domainName): self.domainName = domainName if self.is_cached: self.set_cached() try: my_resolver = dns.resolver.Resolver() my_resolver.nameservers = self.nameservers my_resolver.lifetime = self.timeout answer = my_resolver.query(qname=self.domainName) nameResponse = sorted([ one.address for one in answer ]) except dns.exception.SyntaxError as e: print("dns occurrence SyntaxError: %s" % str(e)) except Exception,e: print(e) else: self._dnscache[self.domainName] = nameResponse return nameResponse class wCurl(object): """docstring for wCurl""" def __init__(self, url, headers): super(wCurl, self).__init__() self.buffer = StringIO.StringIO() self.url = url self.c = pycurl.Curl() self.c.setopt(pycurl.URL, self.url) self.c.setopt(pycurl.CONNECTTIMEOUT, 20) #连接超时时间,20秒 self.c.setopt(pycurl.TIMEOUT, 0) #不设置下载超时时间 self.c.setopt(pycurl.FORBID_REUSE, 1) self.c.setopt(pycurl.MAXREDIRS, 5) # 最大重定向数 self.c.setopt(pycurl.NOPROGRESS, 1) self.c.setopt(pycurl.DNS_CACHE_TIMEOUT,30) self.c.setopt(pycurl.FOLLOWLOCATION,1) self.c.setopt(pycurl.USERAGENT,"Mozilla/5.2 wcurl/Tools") #配置请求HTTP头的User-Agent self.c.setopt(pycurl.HTTPHEADER,headers) # 主机头 self.c.setopt(pycurl.WRITEFUNCTION, self.buffer.write) def get(self): self.METHOD = 'GET' self.REQUUID = uuid.uuid1() try: self.c.perform() except Exception,e: print "connecion error:"+str(e) self.c.close() sys.exit() def post(self, data): self.METHOD = 'POST' self.REQUUID = uuid.uuid1() self.c.setopt(pycurl.POSTFIELDS, urllib.urlencode(data)) try: self.c.perform() except Exception,e: print "connecion error:"+str(e) self.c.close() sys.exit() def sub_dict(form_dict, sub_keys, default=None): return dict([(k, form_dict.get(k.strip(), 0)) for k in sub_keys.split(',')]) @property def NAMELOOKUP_TIME(self): return self.c.getinfo(self.c.NAMELOOKUP_TIME)*1000 @property def CONTENT(self): CONTENT = self.buffer.getvalue() return CONTENT @property def CONNECT_TIME(self): return self.c.getinfo(self.c.CONNECT_TIME)*1000 @property def PRETRANSFER_TIME(self): return self.c.getinfo(self.c.PRETRANSFER_TIME)*1000 @property def STARTTRANSFER_TIME(self): return self.c.getinfo(self.c.STARTTRANSFER_TIME)*1000 @property def TOTAL_TIME(self): return self.c.getinfo(self.c.TOTAL_TIME)*1000 @property def HTTP_CODE(self): return self.c.getinfo(self.c.HTTP_CODE) @property def SIZE_DOWNLOAD(self): return self.c.getinfo(self.c.SIZE_DOWNLOAD)/1024/1024 @property def HEADER_SIZE(self): return self.c.getinfo(self.c.HEADER_SIZE)/1024 @property def SPEED_DOWNLOAD(self): return self.c.getinfo(self.c.SPEED_DOWNLOAD)/1024 def display_dict(self): value_dict = { 'SPEED_DOWNLOAD':"%0.1f kb/s" % self.SPEED_DOWNLOAD, 'NAMELOOKUP_TIME':"%0.1f s" % self.NAMELOOKUP_TIME, 'CONNECT_TIME':"%0.1f s" % self.CONNECT_TIME, 'PRETRANSFER_TIME':"%0.1f s" % self.PRETRANSFER_TIME, 'STARTTRANSFER_TIME':"%0.1f s" % self.STARTTRANSFER_TIME, 'TOTAL_TIME':"%0.1f s" % self.TOTAL_TIME, 'HTTP_CODE':self.HTTP_CODE, 'SIZE_DOWNLOAD':"%0.1f Mb" % self.SIZE_DOWNLOAD, 'HEADER_SIZE': "%0.1f kb" % self.HEADER_SIZE, 'REQ_UUID': self.REQUUID, } # value_dict = self.sub_dict(value_dict,'NAMELOOKUP_TIME,CONNECT_TIME,PRETRANSFER_TIME,STARTTRANSFER_TIME,TOTAL_TIME,HTTP_CODE,SIZE_DOWNLOAD,HEADER_SIZE,SPEED_DOWNLOAD') return value_dict def __repr__(self): return "<%s|%s|%s|%s>" % (self.METHOD,self.url,self.HTTP_CODE,self.REQUUID) if __name__ == '__main__': # print Curl('http://ecp.189.cn/page/app/index.html') # pprint.pprint(Curl('http://www.jczj123.com/user/login.htm?gotoUrl=http%3A%2F%2Fwww.jczj123.com%2Findex2.htm')) # s = _SetDNS(nameservers=['10.0.99.1'],is_cached=False) # s.resolver(domainName="www.baidu.com") # get_ip = s.resolver(domainName='oa.50jjx.com') # print get_ip header = ['host: oa.50jjx.com'] # url = 'http://%s' % get_ip[0] # print url # pprint.pprint(Curl('http://10.0.0.219:8088',header)) # pprint.pprint(Curl('http://bopsbase.xjball.com/login/loginByLoginMode.htm?gotoUrl=http%3A%2F%2Fbopsbase.xjball.com%2F%2Findex.htm')) # pprint.pprint(Curl('http://www.jczj123.com/index2.htm')) # url="http://10.0.0.219:8088/2" wc = wCurl(url,header) wc.post({"data":"1"}) print wc # print wc.display_dict
执行结果:
[u'36.27.209.79'] http://36.27.209.79 <GET|http://36.27.209.79|200|fb2e4baa-bcb6-11e8-b1a0-d481d78c12eb> [Finished in 0.4s] <POST|http://10.0.0.219:8088/2|200|719154a0-bffe-11e8-a63f-d481d78c12eb> [Finished in 0.1s]
checkIPaddress.py
用于检测ip地址的合法性
# -*- set coding:utf-8 -*- ''' Created on 2014 @author: Administrator ''' def ip_check(ip): q = ip.split('.') #return len(q) == 4 and len(filter(lambda x: x >= 0 and x <= 255, map(int, filter(lambda x: x.isdigit(), q)))) == 4 return not len(filter(lambda x: x >= 0 and x <= 255, map(int, filter(lambda x: x.isdigit(), q)))) == 4 import re def ipFormatChk(ip_str): pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" if re.match(pattern, ip_str): return True else: return False str="32.34.56.78" print ipFormatChk(str) print ip_check(str)