时间:2021-05-22
本文实例讲述了Python基本socket通信控制操作。分享给大家供大家参考,具体如下:
python — 基本socket通信控制(控制在celie.txt文件中主机IP地址可以发送信息,并返回对应的客户端IP、时间戳、发送的信息)
客户端代码
root@72129clent:~/python/snmp# lssnmpclenit.py tab.pyroot@72129clent:~/python/snmp# cat snmpclenit.py#!/usr/bin/python# --*-- coding:utf-8 --*--import sockethost,port = '192.168.72.130',18000s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#调用IPv4协议s.connect((host,port))#连接主机与端口s.send("up")#客户端给服务器端发送数据“up”s.close()root@72129clent:~/python/snmp#服务器短信代码:
root@kali:~/python/snmp# lscelie.txt snmpserver.py tab.py tab.pycroot@kali:~/python/snmp# cat celie.txt192.168.72.129 xuweiboroot@kali:~/python/snmp#root@kali:~/python/snmp# cat snmpserver.py#!/usr/bin/python# --*-- coding:utf-8 --*--import datetime#导入时间戳import SocketServer#读取目录下的celie.txt文件host_status = {}#新建字典,使用IP地址作为KEY值。作用是来判断每个客户端IP多久与服务器通信一次的f = open('celie.txt')#调用策略文档,在里面的ip地址就可以通过,并发送信息while True: line = f.readline().split() if len(line) == 0:break print line[0]#打印第一个IP地址信息 host_status[line[0]] = []#给字典第一个设置为空,这样后面只要直接追加值就ok了f.close()class myMonitorHandler(SocketServer.BaseRequestHandler): '''This is the Monitor server''' def handle(self): recv_data = self.request.recv(1024)#接收客户端数据 if self.client_address[0] in host_status.keys():#如果存在字典中的ip地址信息,就返回对应客户端发送的Ip、时间戳、信息 #self.client_address为数组('192.168.72.129', 49109)的值。只要当中的IP地址,因此取self.client_address[0] #把host_status字典中的self.client_address[0]值即IP地址值赋值有两个值,因此新建个列表,存取两个值时间戳与接收的信息 #如:{'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进字典 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)#打印客户端地址、操作的时间戳值与接收的数据 #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,mif __name__ == "__main__":#当自己运行时调用什么什么;当被其他程序调用时调用什么什么,如果被其他程序调用了,下面代码不执行 host,port = '',18000 server = SocketServer.ThreadingTCPServer((host,port),myMonitorHandler)#调用TCP的多线程 server.serve_forever()root@kali:~/python/snmp#脚本运行情况
服务器端运行:
root@kali:~/python/snmp# lsof -i:18000COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEpython 14190 root 3u IPv4 45472 0t0 TCP *:18000 (LISTEN)root@kali:~/python/snmp# kill -9 14190root@kali:~/python/snmp# python snmpserver.py192.168.72.129在celie.txt中192.168.72.129的客户端运行:
root@72129clent:~/python/snmp# python snmpclenit.pyroot@72129clent:~/python/snmp# python snmpclenit.pyroot@72129clent:~/python/snmp# python snmpclenit.pyroot@72129clent:~/python/snmp# python snmpclenit.pyroot@72129clent:~/python/snmp# python snmpclenit.pyroot@72129clent:~/python/snmp#不在celie.txt中192.168.72.1的客户端运行:
192.168.72.1的snmpclinet.py脚本语句:
#!/usr/bin/python# --*-- coding:utf-8 --*--import sockethost,port = '192.168.72.130',18000s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#调用IPv4协议s.connect((host,port))#连接主机与端口s.send("up")#客户端给服务器端发送数据“up”s.close()192.168.72.1的客户端运行:
C:\Python27>python.exe snmpclinet.pyC:\Python27>python.exe snmpclinet.pyC:\Python27>python.exe snmpclinet.pyC:\Python27>python.exe snmpclinet.pyC:\Python27>python.exe snmpclinet.pyC:\Python27>再次查看服务器端运行情况:
root@kali:~/python/snmp# lsof -i:18000COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEpython 14190 root 3u IPv4 45472 0t0 TCP *:18000 (LISTEN)root@kali:~/python/snmp# kill -9 14190root@kali:~/python/snmp# python snmpserver.py192.168.72.129From ('192.168.72.129', 49208) : 2017-08-20 23:31:41.125892 up192.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 up192.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 up192.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 up192.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 up192.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 list192.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 list192.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 list192.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 list192.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 list192.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相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Python提供了两个级别访问的网络服务。低级别的网络服务支持基本的Socket,它提供了标准的BSDSocketsAPI,可以访问底层操作系统Socket接口
本文实例为大家分享了python简易远程控制的具体代码,供大家参考,具体内容如下1.技术:管道通信,流文件处理,socket基础2.Tips:默认IP:127.
本文实例讲述了php实现与python进行socket通信的方法。分享给大家供大家参考,具体如下:设计目的通过前端页面发起请求交给php,php创建socket
以计算机为例,套接字socket的作用是支持TCP/IP的网络通信的基本操作单元,可以看做是不同主机之间的进程进行双向通信的端点,简单的说就是通信的两方的一种约
本文以实例详述了C#实现Socket通信的解决方法,具体实现步骤如下:1、首先打开VS新建两个控制台应用程序:ConsoleApplication_socket