使用,您处在正确的位置CGIHTTPRequestHandler
,因为.htaccess
文件对内置的http服务器没有任何意义。有一个CGIHTTPRequestHandler.cgi_directories
变量,用于指定将可执行文件视为cgi脚本的目录(这里是检查本身)。你应该考虑移动test.py
到一个cgi- bin
或htbin
目录,并使用下面的脚本:
#!/usr/bin/env python3
from http.server import CGIHTTPRequestHandler, HTTPServer
handler = CGIHTTPRequestHandler
handler.cgi_directories = ['/cgi-bin', '/htbin'] # this is the default
server = HTTPServer(('localhost', 8123), handler)
server.serve_forever()
#!/usr/bin/env python3
print('Content-type: text/html\n')
print('<title>Hello World</title>')
您应该最终得到:
|- cgiserver.py
|- cgi-bin/
` test.py
与运行python3 cgiserver.py
并向发送请求localhost:8123/cgi-bin/test.py
。干杯。