代码拉取完成,页面将自动刷新
from socket import *
from time import strftime, localtime
from select import select
from json import loads
import ssl
HOST = ''
POST = eval(input("请输入监听的端口:"))
#POST = 30114
BUFSIZ = input("请输入消息最大长度(默认为1024): ")
if BUFSIZ == '':
BUFSIZ = 1024
else:
BUFSIZ = eval(BUFSIZ)
ADDR = (HOST, POST)
NUM = input("请输入最大连接数:(默认为5) ")
if NUM == '':
NUM = 5
else:
NUM = eval(NUM)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(NUM)
tcpSerSock.setblocking(False)
SerSock = ssl.wrap_socket(tcpSerSock,"key.pem","cert.pem",server_side=True)
inputs = [SerSock]
print('waiting for connnecting...')
while True:
rlist, wlist, xlist = select(inputs, [], [])
for s in rlist:
tcpCliSock = 1
addr = 1
if s is SerSock:
try:
tcpCliSock, addr = s.accept()
except:
pass
if(tcpCliSock != 1 and addr != 1):
print('...connecting from:', addr)
inputs.append(tcpCliSock)
else:
print("connecting-----------------")
data = s.recv(BUFSIZ)
if not data:
inputs.remove(s)
s.close()
continue
obj = loads(data.decode('utf-8'))
time = strftime("%Y-%m-%d %H:%M:%S", localtime())
data = '[{}]{}: {}'.format(time, obj['name'], obj['msg'])
for sock in inputs:
if sock is not SerSock:
sock.send(data.encode('utf-8'))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。