-
Notifications
You must be signed in to change notification settings - Fork 52
/
myTools.py
80 lines (65 loc) · 2.22 KB
/
myTools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: gbk -*-
#__author__ = 'ASUS'
import time,sys, urllib, urllib2, json,socket,re
# 新建一个python文件
def newPy(fname):
fileName=r"E:\资料\onedrive\code\python\opencv"+"\\"+fname;
f = open(fileName, "a");
f.write("# -*- coding: gbk -*-\n#__author__ = 'dragonfive'\n");
# 获取当前时间
now_clock = getSysTime()
f.write("# "+now_clock+"\n")
# 获取当前地点
myip = Getmyip()
address = myip.getAddByIp(myip.get_ex_ip()).encode('gbk');
f.write("# "+address+" dragonfive \n");
# 联系方式
f.write("# any question mail:[email protected] \n");
# 版权信息
f.write("# copyright 1992-"+time.strftime(r"%Y")+ " dragonfive \n");
f.close();
# 获取系统时间
def getSysTime():
now_clock = time.strftime(r"%d/%m/%Y %H:%M:%S")
return now_clock
# 获取百度ipstore的apikey
def getMyApikey():
return "9b447786edf207a8813e115d44d85187"
class Getmyip:
# 获得本机内网IP
def get_local_ip(self):
localIP=socket.gethostbyname(socket.gethostname())
return localIP;
# 获取外网IP
def get_ex_ip(self):
try:
myip = self.visit("http://www.whereismyip.com/")
except:
try:
myip = self.visit("http://www.ip138.com/ip2city.asp")
except:
myip = "So sorry!!!"
return myip
def visit(self,url):
opener = urllib2.urlopen(url)
ourl=opener.geturl()
if url == ourl:
str = opener.read()
asd=re.search('\d+\.\d+\.\d+\.\d+',str).group(0)
return asd
# 获取一个ip的区域地址,用百度ipstore的工具;
def getAddByIp(self,ipaddr):#这里可以用多参数进行优化;
url = 'http://apis.baidu.com/chazhao/ipsearch/ipsearch?ip='+ipaddr
req = urllib2.Request(url)
req.add_header("apikey", getMyApikey());
resp = urllib2.urlopen(req)
content = json.loads(resp.read())
if(content):
address=content["data"]["country"]+":"+content["data"]["city"]+":"+content["data"]["operator"]
return address;
def is_huiwenshu(n):
return n==int(''.join(list(reversed(str(n)))))
def get_huiwenshu():
it=range(100,1000)
it = filter(is_huiwenshu,it)
print([i for i in it if i<1000])