Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e965f9

Browse files
authored
Update get_local_wip.py
1 parent 12db3bf commit 7e965f9

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

get_local_wip.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,69 @@
1-
#encoding: utf-8
1+
#coding:utf-8
2+
3+
import os
4+
import Queue
5+
import codecs
26
import requests
7+
import threading, subprocess
8+
from time import ctime, sleep, time
39
from bs4 import BeautifulSoup
410

5-
6-
# 获取公网出口IP
711
def get_out_ip(url):
8-
r = requests.get(url)
9-
txt = r.text
10-
ip = txt[txt.find("[") + 1: txt.find("]")]
11-
print('ip:' + ip)
12-
return ip
12+
req = requests.get(url)
1313

14+
if req.encoding == 'ISO-8859-1':
15+
encodings = requests.utils.get_encodings_from_content(req.text)
16+
if encodings:
17+
encoding = encodings[0]
18+
else:
19+
encoding = req.apparent_encoding
20+
global encode_content
21+
encode_content = req.content.decode(encoding, 'replace')
22+
23+
return encode_content[encode_content.find("<center>") + 1: encode_content.find("</center>")].replace("center>", "")
1424

1525
def get_real_url(url=r'http://www.ip138.com/'):
1626
r = requests.get(url)
1727
txt = r.text
1828
soup = BeautifulSoup(txt,"html.parser").iframe
1929
return soup["src"]
2030

31+
class ThreadUrl(threading.Thread):
32+
def __init__(self,queue):
33+
threading.Thread.__init__(self)
34+
self.queue = queue
35+
36+
def run(self):
37+
while True:
38+
host = self.queue.get()
39+
ret = subprocess.Popen('ping -n 600 '+ host, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
40+
with open('iflytek/{0}.log'.format(host), 'a') as f:
41+
print >>f, ret.stdout.read()
42+
self.queue.task_done()
43+
44+
def startPing():
45+
for i in range(100):
46+
t=ThreadUrl(queue)
47+
t.setDaemon(True)
48+
t.start()
49+
for host in ['140.143.0.175', '39.107.78.159', '120.92.31.91', '117.121.21.146', '117.121.21.146', '42.62.116.34', '42.62.42.10', '121.201.83.170', '59.107.24.5']:
50+
queue.put(host)
51+
queue.join()
2152

2253
if __name__ == '__main__':
54+
print '*'*40
55+
print '*' + u' 欢迎使用网络质量检测工具 ' + '*'
56+
print '*' + ' ' + '*'
57+
print '*' + u' 程序运行过程中,请不要关闭程序窗口! ' + '*'
58+
print '*'*40
59+
if not os.path.exists('iflytek'):
60+
os.makedirs('iflytek')
2361
ip = get_out_ip(get_real_url())
24-
with open("result.log", "a") as f:
25-
print >>f, ip
62+
f = codecs.open('iflytek/ISP.log', 'w', 'utf-8')
63+
f.write(ip)
64+
queue = Queue.Queue()
65+
ret = startPing()
66+
2667

2768
#pip install pyinstaller
2869
#pyinstaller -F get_local_wip.py >> dist/get_local_wip.exe

0 commit comments

Comments
 (0)