简体   繁体   English

Python简单的Web服务器

[英]Python simple web server

I am trying to to print some simple variables using the built in HTTP server in Python 我试图使用Python内置的HTTP服务器打印一些简单的变量

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

But i seem to get this error: 但我似乎得到这个错误:

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)
----------------------------------------

def do_GET(self): seems to be receiving more then just self, what am i missing? def do_GET(self):似乎接收的只是自我,我缺少什么?

You're passing in a function, when a BaseHTTPRequestHandler is expected. 当您需要BaseHTTPRequestHandler时,您正在传入函数。 In other words, Python is trying to instantiate your object using a BaseHTTPRequestHandler __init__ method, but you've provided a function that takes a different amount of arguments. 换句话说,Python正在尝试使用BaseHTTPRequestHandler __init__方法实例化您的对象,但是您提供了一个带有不同数量参数的函数。

Instead of passing a plain function, sublcass BaseHTTPRequestHandler . 而不是传递普通函数,sublcass BaseHTTPRequestHandler The example linked by @dm03514 in the comments will get you started. @ dm03514在评论中链接的示例将帮助您入门。

Inherit WebServer from a Request Handler like this: 从请求处理程序继承WebServer ,如下所示:

class WebServer(BaseHTTPRequestHandler):

You will probably have to change your imports for that. 您可能必须更改您的导入。

Then pass it as an argument to HTTPServer like this: 然后将它作为参数传递给HTTPServer如下所示:

server = HTTPServer(('', 8080), WebServer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM