在此论坛的其他地方,评论中提供了Python中Web服务器的骨架。我已经抓住了这个跛行,将骨架扩展到我可以在Arduino YUN盾牌上使用的东西,它运行Linux的openWrt风格并且具有Python-2.7.13。
我已经走了一段路,撞到了一堵砖墙。我根据代码顶部的注释发送一个空的GET请求,我得到一个TypeError。 有些Python请指出我明显的错误吗?
以下是服务器收集的控制台输出:
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 638, in __init__
self.handle()
File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/usr/lib/python2.7/BaseHTTPServer.py", line 328, in handle_one_request
method()
File "pythonWebServer.py", line 67, in do_GET
self.sendPage(servedFileName)
TypeError: sendPage() takes exactly 1 argument (2 given)
Starting httpd...
/index.htm
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 41960)
----------------------------------------
Traceback (most recent call last):
File "pythonWebServer.py", line 88, in <module>
run(port=int(argv[1]))
File "pythonWebServer.py", line 82, in run
httpd.serve_forever()
File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever
r, w, e = select.select([self], [], [], poll_interval)
KeyboardInterrupt
以下是产生'TypeError'的代码:
#!/usr/bin/python
"""
Very simple HTTP server in python.
Usage::
./pythonWebServer.py [<port>]
Send a GET request::
curl http://localhost
Send a HEAD request::
curl -I http://localhost
Send a POST request::
curl -d "foo=bar&bin=baz" http://localhost
"""
import os.path
import fileinput
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
servedFileName = "?"
dummyArgs = "Empty"
class S(BaseHTTPRequestHandler):
def sendPage(filePath):
if os.path.exists(filePath):
for line in fileinput.input(filePath):
self.wfile.write(line)
return True
else:
self._set_headers()
self.wfile.write("<html><body><h1> File: "+ filePath + " not found</h1></body></html>")
return False
def pythonSub(subPath, subData):
if os.path.exists(subPath):
# modName = trim(subPath)
# need to strip off .py extension here ??
import subPath
subPath.pyScript(subData)
return True
else:
self._set_headers()
self.wfile.write("<html><body><h1> File: "+ subPath + " not found</h1></body></html>")
return False
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
if self.path == "/":
if os.path.exists("index.html"):
servedFileName = "/index.html"
elif os.path.exists("index.htm"):
servedFileName = "/index.htm"
elif os.path.exists("index.py"):
self.pythonSub("/index.py", dummyArgs)
else:
if instr(self.path, ".py"): # I know what I mean - I'll fix this later.
self.pythonSub(self.path, dummyArgs)
else:
servedFileName = self.path
if servedFileName != "?":
print servedFileName
self.sendPage(servedFileName)
servedFileName = "?"
def do_HEAD(self):
self._set_headers()
def do_POST(self):
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
POSTdata = self.rfile.read(content_length) # <--- Gets the data itself
pythonSub(self.path, POSTdata)
def run(server_class=HTTPServer, handler_class=S, port=80):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting httpd...'
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()