V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Nov 20, 2020 · 2585 views
    This topic created in 1986 days ago, the information mentioned may be changed or developed.
    客户端 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 replies    2020-11-21 19:49:22 +08:00
    est
        1
    est  
       Nov 20, 2020   ❤️ 2
    是谁让你直接拿 wsgiref 开撸接口的,你去怼它。
    GodFastion
        2
    GodFastion  
       Nov 20, 2020 via Android
    python 写接口有更好的选择吧,fastapi flask 都比较轻量好用吧,
    ynkkdev
        3
    ynkkdev  
       Nov 20, 2020
    这就好比你来到了一个米其林餐厅,却选择了自己煮泡面吃
    MoYi123
        4
    MoYi123  
       Nov 20, 2020
    return resp.encode('utf-8') 改成 return [resp.encode('utf-8')] 即可
    julyclyde
        5
    julyclyde  
       Nov 21, 2020
    it must return a write(body_data) callable
    这句话咋理解?
    julyclyde
        6
    julyclyde  
       Nov 21, 2020
    抱歉上面那句是我看错章节了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3070 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 14:22 · PVG 22:22 · LAX 07:22 · JFK 10:22
    ♥ Do have faith in what you're doing.