Python实现根据指定端口探测服务器/模块部署的方法,python部署
Python实现根据指定端口探测服务器/模块部署的方法,python部署
本文实例讲述了Python实现根据指定端口探测服务器/模块部署的方法,非常具有实用价值。分享给大家供大家参考借鉴。
有些时候,在维护过程中,服务器数量非常多。应用模块部署在不同服务器上。有时维护人员做了模块迁移,而未及时同步至手册中。查找比较困难。于是,产生Python根据应用端口进行探测,获取模块部署。
设想非常简单:通过简单的tcp链接,如果能够成功的建立,立即断开,防止影响业务。表示模块在某服务器上有部署。
具体功能代码如下:
#!/bin/env python # import socket import time from threading import Thread hostList=["10.10.126.170","10.10.126.173","10.10.126.177","10.10.126.170","10.10.126.173","10.10.126.177"] onLine=[] offLine=[] gathered=[] hostDict={"onLine":[],"offLine":[]} class detect(Thread): def __init__(self,ip, port=22): Thread.__init__(self) self.ip=ip self.port=port def run(self): address=(self.ip,self.port) sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect(address) buff=sock.recv(1024) if(len(buff)): print("detect Host %s Online" % self.ip) onLine.append(self.ip) except: print("detect Host %s OffLine" % self.ip) offLine.append(self.ip) sock.close def sigle_detect(ip): p=detect(ip) p.start() p.join(60) def multi_detect(host): T_thread=[] for ip in set(host): t=detect(ip) t.name=ip t.start() T_thread.append(t) for t in T_thread: t.join(15) def filter_gather(hlist): gather=[] for t in set(hlist): gather.append(t) return gather def mak_hostList_byip3(iplist): global hostList hostList=[] for ip in set(iplist): tmp=ip.split('.') if(len(tmp)==3): for i in range(2,254): hostList.append('%s.%d' % (ip, i)) elif(len(tmp)==4): hostList.append(ip) else: continue return hostList def update_hostDict(onLine, offLine): hostDict["onLine"]=onLine hostDict["offLine"]=offLine def make_pickle_fileName(): import time fileName="" for s in time.localtime()[:5]: fileName=fileName+str(s) fileName="Host_%s.pkl" % fileName return fileName def save_gathered(fileName, hostDict): import pickle F=open(fileName,'wb') pickle.dump(hostDict,F) F.close() def recovery_gathered(fileName, keyList): import pickle try: F=open(fileName,'rb') E=pickle.load(F) keyList.append(E) except: F.close() return while E: try: E=pickle.load(F) keyList.append(E) except: F.close() break if __name__=='__main__': sigle_detect(hostList[0]) #--------------- mak_hostList_byip3(hostList) multi_detect(hostList) onLine=filter_gather(onLine) print(onLine) offLine=filter_gather(offLine) print(offLine) gathered=onLine+offLine print(gathered) update_hostDict(onLine, offLine) print(hostDict) fN=make_pickle_fileName() save_gathered(fN,hostDict) keyList=[] recovery_gathered(fN,keyList) print(keyList)
希望本文讲述的方法对大家的Python程序设计有所帮助。
#发送端# -*- coding: utf-8 -*-import Tkinterimport tkFileDialogfrom socket import *import structimport osdef client(filename): Addr=('127.0.0.1',8000) BUFSIZE=2048 Sendsocket=socket(AF_INET,SOCK_STREAM) Sendsocket.connect(Addr) FileHead=struct.pack('128s11i',filename,0,0,0,0,0,0,0,0,os.stat(filename).st_size,0,0) Sendsocket.send(FileHead) fp=open(filename,'rb') while 1: FileData=fp.read(BUFSIZE) if not FileData: break Sendsocket.send(FileData) fp.close() Sendsocket.close()def FileOpen(): r = tkFileDialog.askopenfilename(title = 'Python Tkinter', filetypes=[('All files', '*')] ) filename=r.split('/') client(filename[-1]) root = Tkinter.Tk()button1 = Tkinter.Button(root, text = 'File Open', height=8,width=14,bg='blue', command = FileOpen)button1.pack()root.mainloop()#接收端# -*- coding: utf-8 -*-from socket import *import osimport structAddr=('127.0.0.1',8000)BUFSIZE=1024FileInfoSize=struct.calcsize('128s32sI8s')SocketRecv=socket(AF_INET,SOCK_STREAM)SocketRecv.bind(Addr)SocketRecv.listen(5)print "waitting ........"conn,addr=SocketRecv.accept()print "send from" ,addrFileHead=conn.recv(FileInfoSize)filename,temp1,filesize,temp2=struct.unpack('128s32sI8s',FileHead)print filename,len(filename),type(filename)print filesizefilename='new_'+......余下全文>>
可以使用pygtk来实现图形界面,可以在网上找一些教程。
向指定端口发送数据包,使用socket内置库。下面有一个简单的demo以供参考:
server端:
import sockets = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)s.bind(("", 8081))while True: # Receive up to 1,024 bytes in a datagram data, addr = s.recvfrom(1024) print "Received:", data, "from", addrclient端:
import sockets = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)port = 8081host = "localhost"while True: msg = raw_input() s.sendto(msg, (host, port))
望采纳,谢谢支持!
相关内容
- python实现从字符串中找出字符1的位置以及个数的方法,
- python3编写C/S网络程序实例教程,python3实例教程
- python常用web框架简单性能测试结果分享(包含django、fl
- Python3.0与2.X版本的区别实例分析,python3.02.x
- Python实现多行注释的另类方法,python多行
- Python升级提示Tkinter模块找不到的解决方法,pythontkint
- python错误:AttributeError: 'module' object has no attribute 'setd
- Python中还原JavaScript的escape函数编码后字符串的方法,
- python with statement 进行文件操作指南,
- Python with的用法,Pythonwith用法
评论关闭