使用python编写监听端
2018-04-12 10:11
751 查看
本文实例为大家分享了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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Pyjamas:使用Python编写AJAX程序
- 用Python编写shell脚本时经常使用的函数的介绍
- 使用python编写您的微博应用
- 【原创】Python结合PySide使用QT编写ImageViewer[从C++迁移简化]
- 使用Python编写客户端 上传文字or图片至新浪微博
- [zz]使用 Python 为 KVM 编写脚本,第 1 部分: libvirt
- ctypes: 使用python调用C编写的动态链接库
- 使用Python编写客户端 上传文字or图片至新浪微博 by OAuth 2.0
- 使用Python编写免安装运行时、以Windows后台服务形式运行的WEB服务器
- Python使用TCPServer编写(多线程)Socket服务
- 使用 Python 编写 KVM 脚本,第 2 部分: 添加 GUI 来使用 libvirt 和 Python 管理 KVM
- 【转】使用python编写网络通信程序
- 使用python/casperjs编写终极爬虫-客户端App的抓取
- 如何使用Python为Hadoop编写一个简单的MapReduce程序
- 使用Python开发Android应用程序:第三节 在电脑上编写程序在手机上运行
- 使用Python【mincemeat】编写简单的MapReduce程序
- 使用P4D 编写Python Extension
- 使用SWIG轻松编写Python扩展
- 使用Delphi 编写Python Extension
- 使用Delphi 编写Python Extension