推荐学习书目
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 · 2399 views
    This topic created in 3002 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
        1
    ebingtel  
       May 19, 2018
    我帮 lz 测试了一下,linux 命令行 python2.7 下,ok 的……代码没问题
    aduil
        2
    aduil  
    OP
       May 19, 2018
    @ebingtel 我是在 3.6 运行的。代码没报错。但是打开浏览器输入 URL,没有产生预期内容。
    copie
        3
    copie  
       May 20, 2018 via Android
    这个问题在于 response_body 必须是 byte 不是 str
    Qyroth
        4
    Qyroth  
       May 24, 2018
    aduil
        5
    aduil  
    OP
       May 25, 2018
    @copie 嗯,感谢!已经解决!查了一下文档确实如此!
    @jasonqiao36 嗯,感谢!查了一下文档解决了,感觉自己还是文档不太熟。
    aduil
        6
    aduil  
    OP
       May 25, 2018   ❤️ 1
    @ebingtel 在 2.7 的情况下,response_body 使用 str ;在 3.6 的情况下,必须是 byte.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   891 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:15 · PVG 04:15 · LAX 13:15 · JFK 16:15
    ♥ Do have faith in what you're doing.