在工作過程中發現站碼,車道,聯繫人,IP等信息都是在excel,數據庫或者紙質保存,且數據各爲孤島,查詢起來不太方便,因此我用python字典結合tkinter開發出一個小腳本,後面又用pyinstaller 轉換成exe程序(博文在此),這樣一來就可以方便快捷的一鍵查詢了
閒話少說,代碼如下
# -*- coding: UTF-8 -*-
##########################################################################
# SCRIPT_NAME : FindStation.py
# CREATE_TIME : 2019/09/09
# AUTHOR : Mochou
# DESCRIBETION : make station's message find get easy
# PARAMETER : need input station
# UPDATE_RECORD : change run_way from write to input and kill some bugs
#
# DATE OPERATION CZR DESCRIBETION
# ________ _____________ ________ __________________________________
#
# 2019/09/09 UPDATE Mochou
##########################################################################
import os as os
import codecs
import time
import re
from tkinter import *
import subprocess
import threading
# 讀取站名,類型強轉
# station = input("請輸入站碼、站名或拼音: ")
def btn_click():
# 建立字典映射
# 站碼
dict_num = {
'111':['地球站','11.11.11.11','奧特曼','123456789','銀河處','NO'],
'222':['火星站','22.22.22.22','鋼鐵俠','987654321','銀河處','YES']
}
# 站中文名稱
dict_str = {
'地球站':['111','11.11.11.11','奧特曼','123456789','銀河處','NO'],
'火星站':['222','22.22.22.22','鋼鐵俠','987654321','銀河處','YES']
}
# 站碼拼音
dict_word = {
'diqiu':['111','地球站','11.11.11.11','奧特曼','123456789','銀河處','NO'],
'huoxing':['222','火星站','22.22.22.22','鋼鐵俠','987654321','銀河處','YES']
}
# 對錯誤站名做異常處理
try:
# 判斷是站碼、名稱還是拼音
# 使局部變量高可用
global station
station = ipText.get()
# 正則表達式,匹配帶數字的
pattern1 = re.compile('[0-9]+')
match_num = pattern1.findall(station)
pattern2 = re.compile('[a-z]+')
match_word = pattern2.findall(station)
# 站碼
if match_num:
num1 = station
servername = dict_num[num1][0]
dbip = dict_num[num1][1]
linkman = dict_num[num1][2]
linkphone = dict_num[num1][3]
location1 = dict_num[num1][4]
bool1 = dict_num[num1][5]
# 拼音
elif match_word:
num1 = dict_word[station][1]
servername = dict_word[station][0]
dbip = dict_word[station][2]
linkman = dict_word[station][3]
linkphone = dict_word[station][4]
location1 = dict_word[station][5]
bool1 = dict_word[station][6]
# 中文名稱
else:
num1 = dict_str[station][0]
servername = station
dbip = dict_str[station][1]
linkman = dict_str[station][2]
linkphone = dict_str[station][3]
location1 = dict_str[station][4]
bool1 = dict_str[station][5]
ip11 = "銀河處仙女處"
ip22 = "大熊處"
tips = "該站位於11和22服務器,傳輸標誌位flag2=1"
# 判斷IP歸屬哪個服務器
if location1 in ip11:
tips = "該站位於11服務器,傳輸標誌位flag1=1"
elif location1 in ip22:
tips = "該站位於22服務器,傳輸標誌位flag3=1"
else:
tips = tips
# tk的語句插入
text.insert(INSERT,("路站碼:%s\n路站名:%s\n服務器:%s\n聯繫人:%s\n手機號:%s\n歸屬地:%s\n主副站:%s\n其他信息:%s\n\n" %
(num1,servername,dbip,linkman,linkphone,location1,bool1,tips)))
except KeyError:
print ("Error:未找到該站,請查證")
text.insert(INSERT,("未找到該站,請確認!\n"))
# 實現對網絡的ping測試
v = dbip
thread_id = []
j = 1
# 如果未ping通,重試多少次,這裏也可以用字符串組合來ping網段
for i in range(1):
loop = v
thread_id.append(0)
# Ping類裏有兩個參數str_ip,sleep_time
thread_id[i] = Ping(loop, int(j/20))
# 開始調用
thread_id[i].start()
i = i + 1
j = j + 10
# enter調用
def btn_click_enter(self):
btn_click()
# 清空消息
def cleartext():
text.delete('0.0', END)
class Ping(threading.Thread):
def __init__(self,str_ip,sleep_time):
threading.Thread.__init__(self)
self.str_ip = str_ip
self.sleep_time = sleep_time
def run(self):
time.sleep(self.sleep_time)
ftp_ret = subprocess.Popen('ping %s -n 3' % self.str_ip,stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
ret = ftp_ret.stdout.read()
# 這裏的字符集一定是gbk,否則會報錯
str_ret = ret.decode("gbk")
ret_s = re.search("TTL",str_ret)
if ret_s:
text.insert(INSERT,("%s 在線!\n#########################\n" % self.str_ip))
else:
text.insert(INSERT,("%s 無法ping通!\n#########################\n" % self.str_ip))
# 創建窗口對象的背景色
root = Tk()
root.title('便捷式一鍵查詢服務系統')
root.geometry('360x520')
# Frame爲佈局函數
main_frame = Frame(root)
text_frame = Frame(main_frame)
station_frame = Frame(main_frame)
botton_frame = Frame(station_frame)
# 建立列表
l1 = Label(station_frame,text='輸入站名、站碼或拼音')
#l2 = Label(station_frame,text='')
ipText=Entry(station_frame)
# 字體顯示
# ft = tkFont.Font(family='Fixdsys', size=10, weight=tkFont.BOLD)
# pack是加載到窗口
l1.pack(side='left')
ipText.pack(side='left')
ipText['width']=10
#l2.pack(side='left')
'''
兩個函數的意義是既能enter運行,又可以點擊運行,方便操作,擴大使用
bind綁定enter鍵
注意裏面是return 而不是enter
'''
b = Button(station_frame,text='開始',command=btn_click)
b['width']=4
b['height']=1
b.pack(side='left')
ipText.bind("<Return>", btn_click_enter)
# 消息輸入界面
text = Text(text_frame,width = 36, height= 25)
text.pack()
main_frame.pack()
c = Button(text='清空',command=cleartext)
c['width']=4
c['height']=1
c.pack(side='top')
# 輸入框的位置
station_frame.pack(side='top',pady='10')
text_frame.pack()
# 進入消息循環
root.mainloop()
運行結果如下: