python从网络端口读取文本数据
python从网络端口读取文本数据,本范例监控本地12345端口,获取该端口的数据。
""" 作者:皮蛋编程(https://www.pidancode.com) 创建日期:2022/3/25 功能描述:python从网络端口读取文本数据 """ import socket HOST = 'localhost' PORT = 12345 def incoming(host, port): """Open specified port and return file-like object""" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((host, port)) sock.listen(0) request, addr = sock.accept() return request.makefile('r', 0) # /-- network --- for line in incoming(HOST, PORT): print(line)
相关文章