问题描述
这是我的脚本:
#!/usr/bin/python3
import socket
import _thread
WEB_SERVER_VERSION = "1.0"
def on_new_client(clientsocket,addr):
global WEB_SERVER_VERSION
body = ""
body += "HTTP/1.1 200 OK\n\nWelcome to Example Web Server " + WEB_SERVER_VERSION + "\r\n\r\n"
body = bytes(body,encoding="UTF-8")
msg = "";
try:
msg += str(clientsocket.recv(516),encoding="UTF-8")
print(bytes(msg,encoding="UTF-8"))
except Exception as e:
clientsocket.close()
if len(msg) >= 512:
clientsocket.send(b"Exceeded maximum message size supported by the system. Limit the query size to 511 bytes.\r\n")
clientsocket.close()
print(addr,' >> ',bytes(msg,encoding="UTF-8"))
clientsocket.send(body)
clientsocket.close()
s = socket.socket()
host = "192.168.123.123"
port = 8080
print('Server started!')
print('Waiting for clients...')
try:
s.bind((host,port))
except Exception as e:
host = socket.gethostbyname(socket.gethostname())
if not host:
host = "127.0.0.1"
s.bind((host,port))
s.listen(5)
while True:
c,addr = s.accept()
_thread.start_new_thread(on_new_client,(c,addr))
s.close()
所以当客户端请求时:
$ telnet localhost 8080
something
something2
something3
something4
我只想在两个换行符(\r\n\r\n
或 \n\n
)后回复。
我尝试使用循环,但这对我不起作用。请帮忙
我怎样才能做到这一点?
而且,我想设置一个超时时间,这样用户就不能让服务器一直与用户在线。因此,如果用户发送 \r\n\r\n
并结束请求的时间超过 10 秒,则应关闭连接。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)