如何运行CGI“hello world”用python http.server [英] How to run CGI "hello world" with python http.server
问题描述
我使用的是Windows 7和Python 3.4.3。我想在浏览器中运行这个简单的helloworld.py文件:
I am using Windows 7 and Python 3.4.3. I would like to run this simple helloworld.py file in my browser:
print('Content-Type: text/html')
print( '<html>')
print( '<head></head>')
print( '<body>')
print( '<h2>Hello World</h2>')
print( '</body></html>')
我的工作是:
1)转到命令行 C:\Python
(安装python的地方)
1) Go to command line C:\Python
(where python is installed)
2)运行: python -m http.server
3)到Firefox并输入 http:// localhost:8000 / hello.py
3) Got to Firefox and type http://localhost:8000/hello.py
但是,浏览器只打印hello.py文件的内容而不是Hello World。
However, instead of "Hello World", the browser just prints the content of the hello.py file.
怎么能我修好了吗?
推荐答案
来自 http.server
docs :
From the http.server
docs:
CGIHTTPRequestHandler
可以通过传递
- cgi
选项:
$ python3 -m http.server --bind localhost --cgi 8000
将你的脚本放入 cgi_directories
:
Put your script into cgi_directories
:
默认为
['/ cgi-bin','/ htbin']
并描述了被视为包含CGI脚本的目录。
This defaults to
['/cgi-bin', '/htbin']
and describes directories to treat as containing CGI scripts.
在浏览器中打开:
$ python -mwebbrowser http://localhost:8000/cgi-bin/hello.py
其中 hello.py
:
#!/usr/bin/env python3
print("Content-Type: text/html\n")
print("<!doctype html><title>Hello</title><h2>hello world</h2>")
我必须让它可执行POSIX: chmod + x cgi-bin / hello.py
。
I had to make it executable on POSIX: chmod +x cgi-bin/hello.py
.
这篇关于如何运行CGI“hello world”用python http.server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!