[英]How to run CGI “hello world” with python http.server
I am using Windows 7 and Python 3.4.3.我使用的是 Windows 7 和 Python 3.4.3。 I would like to run this simple helloworld.py file in my browser:我想在我的浏览器中运行这个简单的 helloworld.py 文件:
print('Content-Type: text/html')
print( '<html>')
print( '<head></head>')
print( '<body>')
print( '<h2>Hello World</h2>')
print( '</body></html>')
What I do is:我要做的是:
1) Go to command line C:\\Python
(where python is installed) 1)进入命令行C:\\Python
(安装python的地方)
2) run: python -m http.server
2)运行: python -m http.server
3) Got to Firefox and type http://localhost:8000/hello.py
3) 到 Firefox 并输入http://localhost:8000/hello.py
However, instead of "Hello World", the browser just prints the content of the hello.py file.然而,浏览器只是打印 hello.py 文件的内容,而不是“Hello World”。
How can I fix it?我该如何解决?
From the http.server
docs :从http.server
文档:
CGIHTTPRequestHandler
can be enabled in the command line by passing the--cgi
option:CGIHTTPRequestHandler
可以通过传递--cgi
选项在命令行中启用:
$ python3 -m http.server --bind localhost --cgi 8000
Put your script into cgi_directories
:将您的脚本放入cgi_directories
:
This defaults to
['/cgi-bin', '/htbin']
and describes directories to treat as containing CGI scripts.这默认为['/cgi-bin', '/htbin']
并描述要视为包含 CGI 脚本的目录。
Open in the browser:在浏览器中打开:
$ python -mwebbrowser http://localhost:8000/cgi-bin/hello.py
where hello.py
:哪里hello.py
:
#!/usr/bin/env python3
print("Content-Type: text/html\n")
print("<!doctype html><title>Hello</title><h2>hello world</h2>")
I had to make it executable on POSIX: chmod +x cgi-bin/hello.py
.我必须让它在 POSIX 上可执行: chmod +x cgi-bin/hello.py
。
I created a complete example for a friend.我为朋友创建了一个完整的示例。 It is a complete demo you can run with 8 simple copy-paste ready lines of code.这是一个完整的演示,您可以使用 8 行简单的复制粘贴代码来运行。 Enjoy.享受。
echo -e "\n\n Usage: after running this script, visit http://localhost:8000/cgi-bin/hello \n\n"
mkdir /tmp/cgi-bin/
cat > /tmp/cgi-bin/hello <<EOF
#!/bin/bash
echo -e "Content-Type: text/plain\n\n"; date; echo; env
EOF
chmod +x /tmp/cgi-bin/hello
(cd /tmp; python3 -m http.server --cgi 8000)
I did this some time ago for Python2.7我前段时间为 Python2.7 做了这个
from BaseHTTPServer import BaseHTTPRequestHandler
class GetHandler(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
x = self.wfile.write
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
# <--- HTML starts here --->
x("<html>")
# <--- HEAD starts here --->
x("<head>")
x("<title>Title goes here!</title>")
x("</head>")
# <--- HEAD ends here --->
# <--- BODY starts here --->
x("<body>")
x("<p>This is a test.</p>")
x("</body>")
# <--- BODY ends here --->
x("</html>")
# <--- HTML ends here --->
if __name__ == '__main__':
from BaseHTTPServer import HTTPServer
server = HTTPServer(('localhost', 777), GetHandler)
print 'Starting server, use <Ctrl + F2> to stop'
server.serve_forever()
So in Python 3 you just need to change imports所以在 Python 3 中你只需要改变导入
from http.server import BaseHTTPRequestHandler
class GetHandler(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
x = self.wfile.write
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
# <--- HTML starts here --->
x("<html>")
# <--- HEAD starts here --->
x("<head>")
x("<title>Title goes here!</title>")
x("</head>")
# <--- HEAD ends here --->
# <--- BODY starts here --->
x("<body>")
x("<p>This is a test.</p>")
x("</body>")
# <--- BODY ends here --->
x("</html>")
# <--- HTML ends here --->
if __name__ == '__main__':
from http.server import HTTPServer
server = HTTPServer(('localhost', 777), GetHandler)
print('Starting server, use <Ctrl + F2> to stop')
server.serve_forever()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.