V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tuoov
V2EX  ›  Python

求助: Python 用 wsgiref 写数据接口

  •  
  •   tuoov · 2020-11-20 17:59:37 +08:00 · 1768 次点击
    这是一个创建于 1224 天前的主题,其中的信息可能已经有所发展或是发生改变。
    客户端 post 一个带 json 数据的请求,我的处理函数是这样的:

    def save_task(environ, start_response):
    start_response('200 OK', [ ('Content-type','text/plain')])
    text=environ['wsgi.input'].read(int(environ.get("CONTENT_LENGTH", 0)))
    postData=json.loads(text.decode('utf-8'))
    resp = json.dumps({"code ": 0,"message": "成功"},ensure_ascii=False)
    return resp.encode('utf-8')

    现在拿到的数据是没问题的,但返回给客户端的响应状态码永远是 500,程序也不抛出任何异常。请问是哪里出了问题?
    6 条回复    2020-11-21 19:49:22 +08:00
    est
        1
    est  
       2020-11-20 19:30:20 +08:00   ❤️ 2
    是谁让你直接拿 wsgiref 开撸接口的,你去怼它。
    GodFastion
        2
    GodFastion  
       2020-11-20 21:44:46 +08:00 via Android
    python 写接口有更好的选择吧,fastapi flask 都比较轻量好用吧,
    youngce
        3
    youngce  
       2020-11-20 21:49:20 +08:00
    这就好比你来到了一个米其林餐厅,却选择了自己煮泡面吃
    MoYi123
        4
    MoYi123  
       2020-11-20 23:19:51 +08:00
    return resp.encode('utf-8') 改成 return [resp.encode('utf-8')] 即可
    julyclyde
        5
    julyclyde  
       2020-11-21 19:45:18 +08:00
    it must return a write(body_data) callable
    这句话咋理解?
    julyclyde
        6
    julyclyde  
       2020-11-21 19:49:22 +08:00
    抱歉上面那句是我看错章节了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1176 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:27 · PVG 02:27 · LAX 11:27 · JFK 14:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.