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
aduil
V2EX  ›  Python

请教关于 WSGI 的一个问题

  •  
  •   aduil · 2018-05-19 19:14:41 +08:00 · 1832 次点击
    这是一个创建于 2178 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我在 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 条回复    2018-05-25 21:16:05 +08:00
    ebingtel
        1
    ebingtel  
       2018-05-19 20:29:42 +08:00
    我帮 lz 测试了一下,linux 命令行 python2.7 下,ok 的……代码没问题
    aduil
        2
    aduil  
    OP
       2018-05-19 23:12:04 +08:00
    @ebingtel 我是在 3.6 运行的。代码没报错。但是打开浏览器输入 URL,没有产生预期内容。
    copie
        3
    copie  
       2018-05-20 00:06:58 +08:00 via Android
    这个问题在于 response_body 必须是 byte 不是 str
    jasonqiao36
        4
    jasonqiao36  
       2018-05-24 16:30:31 +08:00
    aduil
        5
    aduil  
    OP
       2018-05-25 21:13:42 +08:00
    @copie 嗯,感谢!已经解决!查了一下文档确实如此!
    @jasonqiao36 嗯,感谢!查了一下文档解决了,感觉自己还是文档不太熟。
    aduil
        6
    aduil  
    OP
       2018-05-25 21:16:05 +08:00   ❤️ 1
    @ebingtel 在 2.7 的情况下,response_body 使用 str ;在 3.6 的情况下,必须是 byte.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1715 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:21 · PVG 00:21 · LAX 09:21 · JFK 12:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.