【问题标题】:Getting GET and POST Request data in Python BaseHTTPServer在 Python BaseHTTPServer 中获取 GET 和 POST 请求数据
【发布时间】:2016-05-04 02:49:18
【问题描述】:

我在尝试获取 GET 和 POST 数据时遇到此错误。

Exception happened during processing of request from ('127.0.0.1', 62635)
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 655, in __init__
    self.handle()
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 328, in handle_one_request
    method()
  File "server1.py", line 30, in do_GET
    print form.getvalue["foo"]
TypeError: 'instancemethod' object has no attribute '__getitem__'

代码

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import cgitb; cgitb.enable()
import cgi


class GP(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
    def do_HEAD(self):
        self._set_headers()
    def do_GET(self):
        self._set_headers()
        form = cgi.FieldStorage()
        print form.getvalue["foo"]
        self.wfile.write("<html><body><h1>Get Request Received!</h1></body></html>")
    def do_POST(self):
        self._set_headers()
        form = cgi.FieldStorage()
        print form.getvalue["foo"]
        self.wfile.write("<html><body><h1>POST Request Received!</h1></body></html>")

def run(server_class=HTTPServer, handler_class=GP, port=8088):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print 'Server running at localhost:8088...'
    httpd.serve_forever()

run()

运行代码,我得到了上述错误。这段代码有什么问题?

python server1.py
curl http://localhost:8088?foo=bar
curl -d "foo=bar" http://localhost:8088

【问题讨论】:

    标签: python post get


    【解决方案1】:

    要解决即时错误,您可以使用:

    form.getvalue("foo")
    

    代替:

    form.getvalue["foo"]
    

    导致:

    TypeError: 'instancemethod' object has no attribute '__getitem__'
    

    此外(如https://stackoverflow.com/a/25091973 中所建议的那样),您可能还想使用:

    form = cgi.FieldStorage(
        fp=self.rfile,
        headers=self.headers,
        environ={'REQUEST_METHOD': 'POST'}
    )
    

    代替:

    form = cgi.FieldStorage()
    

    POST 请求处理程序中。

    GET 请求处理程序中,您可能希望使用urlparse.parse_qs(对于Python 2)或urllib.parse.parse_qs(对于Python 3)而不是cgi.FieldStorage

    【讨论】:

      【解决方案2】:

      基于二叉桦树答案的工作版本

      from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
      from urlparse import parse_qs
      import cgi
      
      class GP(BaseHTTPRequestHandler):
          def _set_headers(self):
              self.send_response(200)
              self.send_header('Content-type', 'text/html')
              self.end_headers()
          def do_HEAD(self):
              self._set_headers()
          def do_GET(self):
              self._set_headers()
              print parse_qs(self.path[2:])
              self.wfile.write("<html><body><h1>Get Request Received!</h1></body></html>")
          def do_POST(self):
              self._set_headers()
              form = cgi.FieldStorage(
                  fp=self.rfile,
                  headers=self.headers,
                  environ={'REQUEST_METHOD': 'POST'}
              )
              print form.getvalue("foo")
              self.wfile.write("<html><body><h1>POST Request Received!</h1></body></html>")
      
      def run(server_class=HTTPServer, handler_class=GP, port=8088):
          server_address = ('', port)
          httpd = server_class(server_address, handler_class)
          print 'Server running at localhost:8088...'
          httpd.serve_forever()
      
      run()
      

      【讨论】:

        猜你喜欢
        • 2015-05-02
        • 1970-01-01
        • 2011-12-16
        • 2014-11-21
        • 2020-07-19
        • 2013-05-13
        • 2019-09-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多