我有一个简单的 python 脚本,我想用作网站:
import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
我在那个文件夹里运行
$ python3 -m http.server
然后我去看看 http://hassbian.local:8000/song.py
终端这样说,我将文件作为 txt 文件获取,脚本不会执行。
Serving HTTP on 0.0.0.0 port 8000 ...
192.168.1.115 - - [04/Jun/2017 14:19:59] "GET / HTTP/1.1" 200 -
192.168.1.115 - - [04/Jun/2017 14:20:04] "GET /song.py HTTP/1.1" 200 -
在树莓派上运行
最佳答案
SimpleHTTPServer不做CGI。如果你想要 CGI,你将不得不使用 CGIHTTPServer
This module can run CGI scripts on Unix and Windows systems.
我不确定这是否是您真正想要的,但以您展示的方式调用 python 脚本是 CGI。 CGI 是一种非常古老的做事方式。使用 python 运行简单的网络应用程序现在几乎完全是 webapp2 或 flask 的领域.而涉及数据库的更复杂的应用程序则由 Django 主导。
关于python - 让python脚本在http简单服务器中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44354060/