这篇文章主要介绍了使用python编写监听端的方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
具体内容如下
import socket import time import sys import string import struct import errno import binascii #Definition ser_ip = 'localhost' ser_port = 15001 HEADER_LISTENER = "IIII" split_time = 4 class TcpClient: def run_srv(self): sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) print ("Trying to connect server...") addr = (ser_ip, ser_port) print ("Connecting " + ser_ip + ":" + str(ser_port)) #Connect server try: sock.connect(addr) except Exception,e: print ("Error:%s" % (e)) sock.close() sys.exit() hl = struct.pack(HEADER_LISTENER,0,0,0,0) header_len = len(hl) while True: try: buf_recv = sock.recv(header_len) buf_header = buf_recv[0:header_len] thread_id = struct.unpack("!4I" , buf_header)[0] err_num = struct.unpack("!4I" , buf_header)[1] com_num = struct.unpack("!4I" , buf_header)[2] wait_num = struct.unpack("!4I" , buf_header)[3] #print("header len %d, recv len %d,buf_header:%s,buf_recv:%s")%(header_len,len(buf_recv),binascii.hexlify(buf_header),binascii.hexlify(buf_recv)) print ("split time:%d")%(split_time) print ("thread id :%d")%(thread_id) print ("error nums:%d")%(err_num) print ("compl nums:%d")%(com_num) print ("wait nums:%d")%(wait_num) print ("----------------------") except Exception,e: print ("Error:%s" % (e)) sock.close() sys.exit() if __name__ == '__main__': if (len(sys.argv) >= 2): ser_port = int(sys.argv[1]) client = TcpClient() client.run_srv()
感谢你能够认真阅读完这篇文章,希望小编分享的“使用python编写监听端的方法”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。