程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在后台 Windows 10 Powershell 中运行 Python HTTPServer大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在后台 Windows 10 PowersHell 中运行 Python httpServer?

开发过程中遇到在后台 Windows 10 PowersHell 中运行 Python httpServer的问题如何解决?下面主要结合日常开发的经验,给出你关于在后台 Windows 10 PowersHell 中运行 Python httpServer的解决方法建议,希望对你解决在后台 Windows 10 PowersHell 中运行 Python httpServer有所启发或帮助;

我已经测试了来自其他帖子的 anwers 的几个建议,但我无法像使用其他脚本一样使用 >pythonw server.py 在 powersHell 后台运行我的服务器配置。

我的代码是:

#!/usr/bin/env python3

from http.server import httpServer,SimplehttprequestHandler,test
import socketserver
from urllib.parse import urlparse
from urllib.parse import parse_qs
import requests
import asyncio
import sys,os,signal,threading

class MyhttprequestHandler(SimplehttprequestHandler):
    def end_headers (self):
        self.send_header('Access-Control-Allow-Origin','*')
        SimplehttprequestHandler.end_headers(self)

    def do_GET(self):

        self.send_response(200)

        self.send_header("Content-type","text/HTML")

        self.end_headers()
        HTML = f"31"
        self.wfile.write(bytes(HTML,"utf8"))

def create_server():
    port = 8000
    handler_object = MyhttprequestHandler
    my_server = socketserver.Tcpserver(("",port),handler_object)

    print("serving at port:" + str(port))
    my_server.serve_forever()


threading.Thread(target=create_server).start()

服务器在前台运行正常(从 PW >python server.py 调用)但在后台它不回复。

解决方法

我不确定使用 PowerSHell 是否可以轻松完成,因为 PowerSHell 是交互式 sHell,而不是后台系统。

一些想法。一个比另一个更难。

  1. 使用类似 PyInstaller 的东西将 python 代码编译成 EXE,然后将 EXE 作为 Windows 服务运行(这在最好的情况下很难做到)

  2. 设置定时任务,在登录系统时使用pythonw运行python脚本。此选项可能是两者中更容易的一个,并且可以使用 New-scheduledTask

    通过 PowerSHell 完成

我想第三种方式是通过 IIS,但不幸的是我对此并不熟悉。

编辑:另一个想法!安装 Docker for Windows 并在容器上运行服务器,而不是在 Windows 主机本身上运行。

大佬总结

以上是大佬教程为你收集整理的在后台 Windows 10 Powershell 中运行 Python HTTPServer全部内容,希望文章能够帮你解决在后台 Windows 10 Powershell 中运行 Python HTTPServer所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。