如何为我的python脚本创建本地Web服务器?

时间:2021-06-15 07:15:26

I'm looking to use a local webserver to run a series of python scripts for the user. For various unavoidable reasons, the python script must run locally, not on a server. As a result, I'll be using HTML+browser as the UI, which I'm comfortable with, for the front end.

我希望使用本地Web服务器为用户运行一系列python脚本。出于各种不可避免的原因,python脚本必须在本地运行,而不是在服务器上运行。因此,我将使用HTML +浏览器作为用户界面,我很满意,用于前端。

I've been looking, therefore, for a lightweight web server that can execute python scripts, sitting in the background on a machine, ideally as a Windows service. Security and extensibility are not high priorities as it's all running internally on a small network.

因此,我一直在寻找能够执行python脚本的轻量级Web服务器,它位于机器的后台,理想情况下是Windows服务。安全性和可扩展性不是高优先级,因为它们都在内部在小型网络上运行。

Should I run a native python webserver as a Windows service (in which case, how)? Or is it just as easy to install Apache onto the user's machine and run as CGI? Since this is all local, performance is not an issue either.

我应该将本机python Web服务器作为Windows服务运行(在这种情况下,如何)?或者将Apache安装到用户的计算机上并以CGI身份运行同样容易?由于这都是本地的,性能也不是问题。

Or am I missing something obvious?

还是我错过了一些明显的东西?

3 个解决方案

#1


Don't waste a lot of time creating Windows service.

不要浪费大量时间来创建Windows服务。

Don't waste a lot of time on Windows Apache.

不要在Windows Apache上浪费大量时间。

Just make a Python service that responds to HTTP requests.

只需创建一个响应HTTP请求的Python服务。

Look at https://docs.python.org/2/library/basehttpserver.html
https://docs.python.org/3/library/http.server.html for version 3
Python offers an HTTP server that you can extend with your server-side methods.

查看https://docs.python.org/2/library/basehttpserver.html https://docs.python.org/3/library/http.server.html获取版本3 Python提供了一个可以扩展的HTTP服务器使用服务器端方法。

Look at http://docs.python.org/library/wsgiref.html
Python offers a WSGI reference implementation that makes your server easy and standards-compliant.

查看http://docs.python.org/library/wsgiref.html Python提供了一个WSGI参考实现,使您的服务器易于使用且符合标准。

Also http://fragments.turtlemeat.com/pythonwebserver.php


"I'm trying to avoid making the user run python stuff from the command prompt."

“我试图避免让用户从命令提示符运行python。”

I don't see how clicking a web page is any different from clicking desktop icons.

我没有看到点击网页与点击桌面图标有何不同。

Starting a web server based on Python is relatively easy, once you have the web server. First, build the server. Later, you can make sure the server starts. Let's look at some ways.

一旦拥有了Web服务器,启动基于Python的Web服务器就相对容易了。首先,构建服务器。稍后,您可以确保服务器启动。我们来看看一些方法。

  1. Your user can't use a random browser to open your local page. They need a bookmark to launch "localhost:8000/myspecialserverinsteadofthedestop/" That bookmark can be a .BAT file that (1) runs the server, (2) runs firefox with the proper initial URL.

    您的用户无法使用随机浏览器打开您的本地页面。他们需要一个书签来启动“localhost:8000 / myspecialserverinsteadofthedestop /”这个书签可以是一个.BAT文件,(1)运行服务器,(2)使用正确的初始URL运行firefox。

  2. You can put the server in the user's start-this menu.

    您可以将服务器放在用户的开始 - 此菜单中。

  3. You can make your Python program a windows "service".

    您可以使您的Python程序成为Windows“服务”。

#2


Best way is to make your own local server by using command prompt.

最好的方法是使用命令提示符创建自己的本地服务器。

  1. Make a new folder say Project
  2. 创建一个新文件夹说Project

  3. Make a new folder inside project & name it as "cgi-bin"(without quotes)
  4. 在项目中创建一个新文件夹并将其命名为“cgi-bin”(不带引号)

  5. Paste your .py file inside the cgi-bin folder
  6. 将.py文件粘贴到cgi-bin文件夹中

  7. Open cmd and change to the directory from which you want to run the server and type "python -m CGIHTTPServer"(without quotes)
  8. 打开cmd并切换到要运行服务器的目录,然后键入“python -m CGIHTTPServer”(不带引号)

  9. Minimize the cmd window & open your browser and type "localhost:8000/cgi-bin/yourpythonfilename.py"(without quotes).
  10. 最小化cmd窗口并打开浏览器并键入“localhost:8000 / cgi-bin / yourpythonfilename.py”(不带引号)。

#3


Running a native python webserver as a windows service should be a no brainer. Check out the documentation for writing windows services (win32api, ActiveState python) in python and also the documentation for subclassing BaseHttpServer and SimpleHttpServer.

将本机python Web服务器作为Windows服务运行应该是没有道理的。查看用于在python中编写Windows服务(win32api,ActiveState python)的文档以及子类化BaseHttpServer和SimpleHttpServer的文档。

BTW: I had a similar question on *: How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?

BTW:我在*上有一个类似的问题:如何在BaseHTTPRequestHandler子类中停止BaseHTTPServer.serve_forever()?

Basically, you subclass BaseHTTPServer (you have to anyway...) and then... but just read the accepted answer - it set me on the right track!

基本上,你是BaseHTTPServer的子类(无论如何......)然后......但只是阅读了接受的答案 - 它让我走上正轨!

#1


Don't waste a lot of time creating Windows service.

不要浪费大量时间来创建Windows服务。

Don't waste a lot of time on Windows Apache.

不要在Windows Apache上浪费大量时间。

Just make a Python service that responds to HTTP requests.

只需创建一个响应HTTP请求的Python服务。

Look at https://docs.python.org/2/library/basehttpserver.html
https://docs.python.org/3/library/http.server.html for version 3
Python offers an HTTP server that you can extend with your server-side methods.

查看https://docs.python.org/2/library/basehttpserver.html https://docs.python.org/3/library/http.server.html获取版本3 Python提供了一个可以扩展的HTTP服务器使用服务器端方法。

Look at http://docs.python.org/library/wsgiref.html
Python offers a WSGI reference implementation that makes your server easy and standards-compliant.

查看http://docs.python.org/library/wsgiref.html Python提供了一个WSGI参考实现,使您的服务器易于使用且符合标准。

Also http://fragments.turtlemeat.com/pythonwebserver.php


"I'm trying to avoid making the user run python stuff from the command prompt."

“我试图避免让用户从命令提示符运行python。”

I don't see how clicking a web page is any different from clicking desktop icons.

我没有看到点击网页与点击桌面图标有何不同。

Starting a web server based on Python is relatively easy, once you have the web server. First, build the server. Later, you can make sure the server starts. Let's look at some ways.

一旦拥有了Web服务器,启动基于Python的Web服务器就相对容易了。首先,构建服务器。稍后,您可以确保服务器启动。我们来看看一些方法。

  1. Your user can't use a random browser to open your local page. They need a bookmark to launch "localhost:8000/myspecialserverinsteadofthedestop/" That bookmark can be a .BAT file that (1) runs the server, (2) runs firefox with the proper initial URL.

    您的用户无法使用随机浏览器打开您的本地页面。他们需要一个书签来启动“localhost:8000 / myspecialserverinsteadofthedestop /”这个书签可以是一个.BAT文件,(1)运行服务器,(2)使用正确的初始URL运行firefox。

  2. You can put the server in the user's start-this menu.

    您可以将服务器放在用户的开始 - 此菜单中。

  3. You can make your Python program a windows "service".

    您可以使您的Python程序成为Windows“服务”。

#2


Best way is to make your own local server by using command prompt.

最好的方法是使用命令提示符创建自己的本地服务器。

  1. Make a new folder say Project
  2. 创建一个新文件夹说Project

  3. Make a new folder inside project & name it as "cgi-bin"(without quotes)
  4. 在项目中创建一个新文件夹并将其命名为“cgi-bin”(不带引号)

  5. Paste your .py file inside the cgi-bin folder
  6. 将.py文件粘贴到cgi-bin文件夹中

  7. Open cmd and change to the directory from which you want to run the server and type "python -m CGIHTTPServer"(without quotes)
  8. 打开cmd并切换到要运行服务器的目录,然后键入“python -m CGIHTTPServer”(不带引号)

  9. Minimize the cmd window & open your browser and type "localhost:8000/cgi-bin/yourpythonfilename.py"(without quotes).
  10. 最小化cmd窗口并打开浏览器并键入“localhost:8000 / cgi-bin / yourpythonfilename.py”(不带引号)。

#3


Running a native python webserver as a windows service should be a no brainer. Check out the documentation for writing windows services (win32api, ActiveState python) in python and also the documentation for subclassing BaseHttpServer and SimpleHttpServer.

将本机python Web服务器作为Windows服务运行应该是没有道理的。查看用于在python中编写Windows服务(win32api,ActiveState python)的文档以及子类化BaseHttpServer和SimpleHttpServer的文档。

BTW: I had a similar question on *: How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?

BTW:我在*上有一个类似的问题:如何在BaseHTTPRequestHandler子类中停止BaseHTTPServer.serve_forever()?

Basically, you subclass BaseHTTPServer (you have to anyway...) and then... but just read the accepted answer - it set me on the right track!

基本上,你是BaseHTTPServer的子类(无论如何......)然后......但只是阅读了接受的答案 - 它让我走上正轨!