怎么在Python中实现socket通信-创新互联
这期内容当中小编将会给大家带来有关怎么在Python中实现socket通信,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联专注于武穴企业网站建设,成都响应式网站建设公司,成都商城网站开发。武穴网站建设公司,为武穴等地区提供建站服务。全流程定制网站开发,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务客户端代码
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)#调用IPv4协议 s.connect((host,port))#连接主机与端口 s.send("up")#客户端给服务器端发送数据“up” s.close() root@72129clent:~/python/snmp#
服务器短信代码:
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#导入时间戳 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,m if __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: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的客户端运行:
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的客户端运行:
192.168.72.1的snmpclinet.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)#调用IPv4协议 s.connect((host,port))#连接主机与端口 s.send("up")#客户端给服务器端发送数据“up” s.close()
192.168.72.1的客户端运行:
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>
再次查看服务器端运行情况:
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中实现socket通信了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享文章:怎么在Python中实现socket通信-创新互联
本文地址:http://pwwzsj.com/article/dheodo.html