【问题标题】:Handling POST requests using Pythons web.py使用 Pythons web.py 处理 POST 请求
【发布时间】:2016-03-16 12:25:46
【问题描述】:

我有一个非常简单的服务器。我使用 Python 2.7 和 web.py。

基本上,我的代码如下所示:

urls = ("/endpoint", "Endpoint")

class Endpoint(object):
    def GET(self):
        return "End point"
    def POST(self):
        data = web.data()
        web.header('Content-Type', 'application/json')
        result = json.loads(data)
        logging.info("[Server] Endpoint POST with payload: " + json.dumps(result))
        return "Endpoint POST"

我通过发出这样的 POST 请求来测试这个服务器:

echo '{"field": "test"}' | curl -d @- http://my.ip.number:port/endpoint

我尝试了服务器发出 POST 请求的其他方法。我还尝试从终端和浏览器发出 get 请求。

在所有情况下,我都会收到这个非常奇怪的错误。

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 239, in process
    return self.handle()
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 229, in handle
    fn, args = self._match(self.mapping, web.ctx.path)
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 427, in _match
    for pat, what in mapping:
ValueError: need more than 1 value to unpack

为什么会发生这个错误,我可以做些什么来防止它?

谢谢!

编辑 1:

显示回溯后,我也得到了这个:

192.168.46.1:51390 - - [16/Mar/2016 12:54:08] "HTTP/1.1 GET /favicon.ico" - 500 Internal Server Error

IP 和端口都不是我使用的。

编辑 2

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Unicode
from __future__ import unicode_literals

import sys
reload(sys)
# -----

# Logging
import logging
logging.basicConfig(level=logging.INFO)
# -----

# Libs
import web
import json
# -----

urls = ("/", "Index",
        "/endpoint1", "EP1"
        "/endpoint2", "EP2")

class Index(object):
    # In the browser, this displays "Index", but also causes the error on the server side.
    def GET(self):
        return "Index"
    # Doesn't do anything, but causes the error
    def POST(self):
        data = web.data()
        web.header('Content-Type', 'application/json')
        result = json.loads(data)

        logging.info("[Server] Index " + json.dumps(result))
        return "Index POST"

class EP1(object):
    def GET(self):
        return "EP1"
    def POST(self):
        data = web.data()
        web.header('Content-Type', 'application/json')
        result = json.loads(data)

        logging.info("[Server] EP1 " + json.dumps(result))
        return "EP1 POST"


class EP2(object):
    def GET(self):
        return "EP2"
    def POST(self):
        data = web.data()
        web.header('Content-Type', 'application/json')
        result = json.loads(data)

        logging.info("[Server] EP2 " + json.dumps(result))
        return "EP2 POST"

if __name__ == "__main__":
    logging.info("[Server] Starting server.")
    app = web.application(urls, globals())
    app.run()

这就是我的服务器的样子。

我这样启动服务器:

python server.py 0.0.0.0:7331

如果我从浏览器访问服务器的根端点,我会得到“索引”并且错误仍然存​​在。其他两个端点不返回任何内容并导致错误。

【问题讨论】:

  • 您的 POST 方法没有返回任何内容。
  • 我也改了,我会更新代码。即使我添加 return "Endpoint POST" 我仍然会收到错误。
  • 请发布更多代码。看来您的applications mapping 是错误的。它应该是您在 urls 中定义的内容,但请发布您创建 application 的行。
  • @Sevanteri 好的,我会发布整个代码

标签: python python-2.7 rest httprequest web.py


【解决方案1】:

您在此处的第二行缺少逗号:

urls = ("/", "Index",
        "/endpoint1", "EP1"
        "/endpoint2", "EP2")

应该是这样的:

urls = ("/", "Index",
        "/endpoint1", "EP1",
        "/endpoint2", "EP2")

没有逗号的情况是 Python 连接两个字符串,中间没有逗号。

所以对于你的代码,urls 实际上是

("/", "Index", "/endpoint1", "EP1/endpoint2", "EP2")

【讨论】:

    猜你喜欢
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    相关资源
    最近更新 更多