加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.py 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
当年万里觅封侯 提交于 2024-02-20 12:36 . 运行代码
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'))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化