|
1 |
| -#encoding: utf-8 |
| 1 | +#coding:utf-8 |
| 2 | + |
| 3 | +import os |
| 4 | +import Queue |
| 5 | +import codecs |
2 | 6 | import requests
|
| 7 | +import threading, subprocess |
| 8 | +from time import ctime, sleep, time |
3 | 9 | from bs4 import BeautifulSoup
|
4 | 10 |
|
5 |
| - |
6 |
| -# 获取公网出口IP |
7 | 11 | 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) |
13 | 13 |
|
| 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>", "") |
14 | 24 |
|
15 | 25 | def get_real_url(url=r'http://www.ip138.com/'):
|
16 | 26 | r = requests.get(url)
|
17 | 27 | txt = r.text
|
18 | 28 | soup = BeautifulSoup(txt,"html.parser").iframe
|
19 | 29 | return soup["src"]
|
20 | 30 |
|
| 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() |
21 | 52 |
|
22 | 53 | 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') |
23 | 61 | 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 | + |
26 | 67 |
|
27 | 68 | #pip install pyinstaller
|
28 | 69 | #pyinstaller -F get_local_wip.py >> dist/get_local_wip.exe
|
0 commit comments