我在Windows 10 Pro x64上的Python 3.6中实现基本HTTPServer时遇到问题。
基本上,socketserver.serve_forever()
的基本实现中使用的选择器使用选择器来确定套接字是否可读。问题在于服务器收到单个GET请求后,它始终评估为true。因此,让我们以接收单个GET请求的情况为例。处理了GET,刷新了套接字,但是selector.select
仍返回true,因此服务器尝试读取空套接字,导致其挂起并阻塞。
socketserver.serveforever:
def serve_forever(self, poll_interval=0.5):
"""Handle one request at a time until shutdown.
Polls for shutdown every poll_interval seconds. Ignores
self.timeout. If you need to do periodic tasks, do them in
another thread.
"""
self.__is_shut_down.clear()
try:
# XXX: Consider using another file descriptor or connecting to the
# socket to wake this up instead of polling. Polling reduces our
# responsiveness to a shutdown request and wastes cpu at all other
# times.
with _ServerSelector() as selector:
selector.register(self, selectors.EVENT_READ)
while not self.__shutdown_request:
ready = selector.select(poll_interval)
if ready:
self._handle_request_noblock()
self.service_actions()
finally:
self.__shutdown_request = False
self.__is_shut_down.set()
选择器。选择:
if sys.platform == 'win32':
def _select(self, r, w, _, timeout=None):
r, w, x = select.select(r, w, w, timeout)
return r, w + x, []
else:
_select = select.select
def select(self, timeout=None):
timeout = None if timeout is None else max(timeout, 0)
ready = []
try:
r, w, _ = self._select(self._readers, self._writers, [], timeout)
except InterruptedError:
return ready
r = set(r)
w = set(w)
for fd in r | w:
events = 0
if fd in r:
events |= EVENT_READ
if fd in w:
events |= EVENT_WRITE
key = self._key_from_fd(fd)
if key:
ready.append((key, events & key.events))
return ready
这似乎是selector
中的这一行。一旦收到GET请求,它将永久返回r
的值:
r, w, x = select.select(r, w, w, timeout)
。
** EDIT1失败的基本实现
MyServer.py
import http
from http import server
class Server(server.HTTPServer):
pass
MyRequestHandler.py
from http.server import BaseHTTPRequestHandler
import urllib
class OAuthGrantRequestHandler(BaseHTTPRequestHandler):
"""docstring"""
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
parts = urllib.parse.urlparse(self.path)
self.wfile.write(
b'<html><head><title>Authentication Status</title></head>'
b'<body><p>The authentication flow has completed.</p>')
print("Request handler completed")
return
MyTest.py
import MyServer
import MyRequestHandler
import threading
def AwaitCallback(server_class=MyServer.Server,
handler_class=MyRequestHandler.OAuthGrantRequestHandler):
"""docstring"""
server_address = ("127.0.0.1", 8080)
Httpd = server_class(server_address, handler_class)
Httpd.timeout = 200
t1 = threading.Thread(target=Httpd.serve_forever, args=(1,))
try:
t1.start()
finally:
if t1:
t1.join()
print("thread 3 terminated")
if Httpd:
Httpd.server_close()
return
AwaitCallback()
运行测试后,我只是将localhost:8080放到我最喜欢的浏览器和wallah中,服务器成功响应然后锁定。
答案 0 :(得分:0)
在选择器上。选择 改变
def _select(self, r, w, _, timeout=None):
进入
def _select(self, r, w, x, timeout=None):