我試圖打印使用內置的HTTP服務器的一些簡單的變量在PythonPython的簡單Web服務器
class WebServer:
def __init__(self):
from BaseHTTPServer import HTTPServer
import urlparse
server = HTTPServer(('', 8080), self.do_GET)
server.serve_forever()
def do_GET(self):
parsed_path = urlparse.urlparse(self.path)
message_parts = [
'CLIENT VALUES:',
'client_address=%s (%s)' % (self.client_address, self.address_string()),
'command=%s' % self.command,
'path=%s' % self.path,
'real path=%s' % parsed_path.path,
'query=%s' % parsed_path.query,
'request_version=%s' % self.request_version,
'',
'SERVER VALUES:',
'server_version=%s' % self.server_version,
'sys_version=%s' % self.sys_version,
'protocol_version=%s' % self.protocol_version,
'',
'HEADERS RECEIVED:',
]
for name, value in sorted(self.headers.items()):
message_parts.append('%s=%s' % (name, value.rstrip()))
message_parts.append('')
message = '\r\n'.join(message_parts)
self.send_response(200)
self.end_headers()
self.wfile.write(message)
return
但我似乎得到這個錯誤:
Exception happened during processing of request from ('10.0.1.3', 52251)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
TypeError: do_GET() takes exactly 1 argument (4 given)
----------------------------------------
高清do_GET(個體經營):似乎接受更多然後只是自我,我錯過了什麼?
你不需要一個'HandlerClass'? – imreal
在這種情況下是不是self.do_GET? – HyperDevil
看看你正在使用的模塊的文檔:)當你需要一個完全成熟的對象時,你需要傳遞一個函數http://docs.python.org/2/library/basehttpserver.html#BaseHTTPServer。 BaseHTTPRequestHandler – slezica