【发布时间】:2021-08-17 04:27:57
【问题描述】:
我正在创建一个 pyqt 应用程序,它将启动一个 Web 服务器并通过 QWebEngineView 进行查看。
它使用 http.server 来创建网络服务器。
它还使用多线程来创建服务器。
这是我的应用程序代码。
import sys
import threading
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setWindowTitle("Medicine")
self.setWindowIcon(QIcon("icon.png"))
layout = QVBoxLayout()
self.setLayout(layout)
web = WebPage()
layout.addWidget(web)
class WebPage(QWebEngineView):
def __init__(self):
QWebEngineView.__init__(self)
self.load(QUrl("http://127.0.0.1:8000"))
app = QApplication(sys.argv)
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
message = "Hello, World!"
self.wfile.write(bytes(message, "utf8"))
with HTTPServer(('', 8000), Handler) as server:
t = threading.Thread(target=server.serve_forever)
t.start()
def startapp():
screen = Window()
screen.show()
sys.exit(app.exec_())
startapp()
但它输出错误:
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\socketserver.py", line 232, in serve_forever
ready = selector.select(poll_interval)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\selectors.py", line 324, in select
r, w, _ = self._select(self._readers, self._writers, [], timeout)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\selectors.py", line 315, in _select
r, w, x = select.select(r, w, w, timeout)
OSError: [WinError 10038] An operation was attempted on something that is not a socket
【问题讨论】:
标签: python python-3.x multithreading pyqt5 python-sockets