Qt and Web server app OSError: [WinError 10038] An operation was attempted on something that is not a socket

Qt and Web server app OSError: [WinError 10038] An operation was attempted on something that is not a socket

我正在创建一个 pyqt 应用程序,它将启动一个网络服务器并通过 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

问题是您正在启动连接并立即关闭它。逻辑是在事件循环为 运行:

时保持打开状态
import sys
import threading

from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView

from http.server import BaseHTTPRequestHandler, HTTPServer


class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.setWindowTitle("Medicine")
        self.setWindowIcon(QIcon("icon.png"))
        layout = QVBoxLayout(self)
        web = WebPage()
        layout.addWidget(web)


class WebPage(QWebEngineView):
    def __init__(self):
        QWebEngineView.__init__(self)
        self.load(QUrl("http://127.0.0.1:8000"))


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"))


def startapp():
    ret = 0
    app = QApplication(sys.argv)

    with HTTPServer(("", 8000), Handler) as server:
        t = threading.Thread(target=server.serve_forever, daemon=True)
        t.start()

        screen = Window()
        screen.show()

        ret = app.exec_()
    sys.exit(ret)


startapp()