您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在Python中實(shí)現(xiàn)socket通信,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
客戶端代碼
root@72129clent:~/python/snmp# ls snmpclenit.py tab.py root@72129clent:~/python/snmp# cat snmpclenit.py #!/usr/bin/python # --*-- coding:utf-8 --*-- import socket host,port = '192.168.72.130',18000 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#調(diào)用IPv4協(xié)議 s.connect((host,port))#連接主機(jī)與端口 s.send("up")#客戶端給服務(wù)器端發(fā)送數(shù)據(jù)“up” s.close() root@72129clent:~/python/snmp#
服務(wù)器短信代碼:
root@kali:~/python/snmp# ls celie.txt snmpserver.py tab.py tab.pyc root@kali:~/python/snmp# cat celie.txt 192.168.72.129 xuweibo root@kali:~/python/snmp# root@kali:~/python/snmp# cat snmpserver.py #!/usr/bin/python # --*-- coding:utf-8 --*-- import datetime#導(dǎo)入時(shí)間戳 import SocketServer #讀取目錄下的celie.txt文件 host_status = {}#新建字典,使用IP地址作為KEY值。作用是來(lái)判斷每個(gè)客戶端IP多久與服務(wù)器通信一次的 f = open('celie.txt')#調(diào)用策略文檔,在里面的ip地址就可以通過(guò),并發(fā)送信息 while True: line = f.readline().split() if len(line) == 0:break print line[0]#打印第一個(gè)IP地址信息 host_status[line[0]] = []#給字典第一個(gè)設(shè)置為空,這樣后面只要直接追加值就ok了 f.close() class myMonitorHandler(SocketServer.BaseRequestHandler): '''This is the Monitor server''' def handle(self): recv_data = self.request.recv(1024)#接收客戶端數(shù)據(jù) if self.client_address[0] in host_status.keys():#如果存在字典中的ip地址信息,就返回對(duì)應(yīng)客戶端發(fā)送的Ip、時(shí)間戳、信息 #self.client_address為數(shù)組('192.168.72.129', 49109)的值。只要當(dāng)中的IP地址,因此取self.client_address[0] #把host_status字典中的self.client_address[0]值即IP地址值賦值有兩個(gè)值,因此新建個(gè)列表,存取兩個(gè)值時(shí)間戳與接收的信息 #如:{'192.168.72.129': [(datetime.datetime(2017, 8, 20, 21, 29, 59, 415054), 'up')]} #host_status[self.client_address[0]] = [(datetime.datetime.now(),recv_data)] #直接把元組append進(jìn)字典 host_status[self.client_address[0]].append((datetime.datetime.now(),recv_data)) print 'From %s : %s %s' %(self.client_address,datetime.datetime.now(),recv_data)#打印客戶端地址、操作的時(shí)間戳值與接收的數(shù)據(jù) #print host_status else:#不存在字典中,則如下提示信息 print "sorry, ip %s is not in the monitor list" % self.client_address[0] #打印出192.168.72.129 [(datetime.datetime(2017, 8, 20, 22, 1, 6, 705498), 'up')] for t,m in host_status.items(): print t,m if __name__ == "__main__":#當(dāng)自己運(yùn)行時(shí)調(diào)用什么什么;當(dāng)被其他程序調(diào)用時(shí)調(diào)用什么什么,如果被其他程序調(diào)用了,下面代碼不執(zhí)行 host,port = '',18000 server = SocketServer.ThreadingTCPServer((host,port),myMonitorHandler)#調(diào)用TCP的多線程 server.serve_forever() root@kali:~/python/snmp#
腳本運(yùn)行情況
服務(wù)器端運(yùn)行:
root@kali:~/python/snmp# lsof -i:18000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python 14190 root 3u IPv4 45472 0t0 TCP *:18000 (LISTEN) root@kali:~/python/snmp# kill -9 14190 root@kali:~/python/snmp# python snmpserver.py 192.168.72.129
在celie.txt中192.168.72.129的客戶端運(yùn)行:
root@72129clent:~/python/snmp# python snmpclenit.py root@72129clent:~/python/snmp# python snmpclenit.py root@72129clent:~/python/snmp# python snmpclenit.py root@72129clent:~/python/snmp# python snmpclenit.py root@72129clent:~/python/snmp# python snmpclenit.py root@72129clent:~/python/snmp#
不在celie.txt中192.168.72.1的客戶端運(yùn)行:
192.168.72.1的snmpclinet.py腳本語(yǔ)句:
#!/usr/bin/python # --*-- coding:utf-8 --*-- import socket host,port = '192.168.72.130',18000 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#調(diào)用IPv4協(xié)議 s.connect((host,port))#連接主機(jī)與端口 s.send("up")#客戶端給服務(wù)器端發(fā)送數(shù)據(jù)“up” s.close()
192.168.72.1的客戶端運(yùn)行:
C:\Python27>python.exe snmpclinet.py C:\Python27>python.exe snmpclinet.py C:\Python27>python.exe snmpclinet.py C:\Python27>python.exe snmpclinet.py C:\Python27>python.exe snmpclinet.py C:\Python27>
再次查看服務(wù)器端運(yùn)行情況:
root@kali:~/python/snmp# lsof -i:18000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python 14190 root 3u IPv4 45472 0t0 TCP *:18000 (LISTEN) root@kali:~/python/snmp# kill -9 14190 root@kali:~/python/snmp# python snmpserver.py 192.168.72.129 From ('192.168.72.129', 49208) : 2017-08-20 23:31:41.125892 up 192.168.72.129 [(datetime.datetime(2017, 8, 20, 23, 31, 41, 125872), 'up')] From ('192.168.72.129', 49209) : 2017-08-20 23:31:57.141410 up 192.168.72.129 [(datetime.datetime(2017, 8, 20, 23, 31, 41, 125872), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 141389), 'up')] From ('192.168.72.129', 49210) : 2017-08-20 23:31:57.747056 up 192.168.72.129 [(datetime.datetime(2017, 8, 20, 23, 31, 41, 125872), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 141389), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 747038), 'up')] From ('192.168.72.129', 49211) : 2017-08-20 23:31:58.394295 up 192.168.72.129 [(datetime.datetime(2017, 8, 20, 23, 31, 41, 125872), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 141389), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 747038), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 394273), 'up')] From ('192.168.72.129', 49212) : 2017-08-20 23:31:58.887359 up 192.168.72.129 [(datetime.datetime(2017, 8, 20, 23, 31, 41, 125872), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 141389), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 747038), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 394273), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 887340), 'up')] sorry, ip 192.168.72.1 is not in the monitor list 192.168.72.129 [(datetime.datetime(2017, 8, 20, 23, 31, 41, 125872), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 141389), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 747038), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 394273), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 887340), 'up')] sorry, ip 192.168.72.1 is not in the monitor list 192.168.72.129 [(datetime.datetime(2017, 8, 20, 23, 31, 41, 125872), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 141389), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 747038), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 394273), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 887340), 'up')] sorry, ip 192.168.72.1 is not in the monitor list 192.168.72.129 [(datetime.datetime(2017, 8, 20, 23, 31, 41, 125872), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 141389), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 747038), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 394273), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 887340), 'up')] sorry, ip 192.168.72.1 is not in the monitor list 192.168.72.129 [(datetime.datetime(2017, 8, 20, 23, 31, 41, 125872), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 141389), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 747038), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 394273), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 887340), 'up')] sorry, ip 192.168.72.1 is not in the monitor list 192.168.72.129 [(datetime.datetime(2017, 8, 20, 23, 31, 41, 125872), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 141389), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 57, 747038), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 394273), 'up'), (datetime.datetime(2017, 8, 20, 23, 31, 58, 887340), 'up')]
上述就是小編為大家分享的怎么在Python中實(shí)現(xiàn)socket通信了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。