推荐学习书目
› 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
aduil
V2EX  ›  Python

请教关于 WSGI 的一个问题

  •  
  •   aduil · May 19, 2018 · 2541 views
    This topic created in 3096 days ago, the information mentioned may be changed or developed.
    我在 IDE 中输入如下代码运行,然后在浏览器中输入 http://127.0.0.1:8051/,却显示 ”该网页无法正常运作 127.0.0.1 未发送任何数据。”。
    from wsgiref.simple_server import make_server

    def application(environ, start_response):
    path = environ.get('PATH_INFO')
    if path == '/':
    response_body = "Index"
    else:
    response_body = "Hello"
    status = "200 OK"
    response_headers = [("Content-Length", str(len(response_body)))]
    start_response(status, response_headers)
    return [response_body]

    httpd = make_server('127.0.0.1', 8051, application)

    httpd.serve_forever()
    6 replies  •  2018-05-25 21:16:05 +08:00
    ebingtel
    ebingtel
    May 19, 2018
    1
    我帮 lz 测试了一下,linux 命令行 python2.7 下,ok 的……代码没问题
    aduil
    aduil
    OP
    May 19, 2018
    2
    @ebingtel 我是在 3.6 运行的。代码没报错。但是打开浏览器输入 URL,没有产生预期内容。
    copie
    copie
    May 20, 2018 via Android
    3
    这个问题在于 response_body 必须是 byte 不是 str
    aduil
    aduil
    OP
    May 25, 2018
    5
    @copie 嗯,感谢!已经解决!查了一下文档确实如此!
    @jasonqiao36 嗯,感谢!查了一下文档解决了,感觉自己还是文档不太熟。
    aduil
    aduil
    OP
    May 25, 2018❤️1
    6
    @ebingtel 在 2.7 的情况下,response_body 使用 str ;在 3.6 的情况下,必须是 byte.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1108 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:05 · PVG 07:05 · LAX 15:05 · JFK 18:05
    ♥ Do have faith in what you're doing.