Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpy 跑不起来静态文件,为何? #18

Open
fishenal opened this issue Jun 11, 2014 · 6 comments
Open

webpy 跑不起来静态文件,为何? #18

fishenal opened this issue Jun 11, 2014 · 6 comments

Comments

@fishenal
Copy link

Traceback (most recent call last):
File "D:\python 2.7.7\lib\site-packages\web\wsgiserver__init__.py", line 1245, in communicate
req.respond()
File "D:\python 2.7.7\lib\site-packages\web\wsgiserver__init__.py", line 775, in respond
self.server.gateway(self).respond()
File "D:\python 2.7.7\lib\site-packages\web\wsgiserver__init__.py", line 2020, in respond
for chunk in response:
File "D:\python 2.7.7\lib\site-packages\web\httpserver.py", line 247, in iter
self.start_response(self.status, self.headers)
File "D:\python 2.7.7\lib\site-packages\web\httpserver.py", line 302, in xstart_response
out = start_response(status, response_headers, *args)
File "D:\python 2.7.7\lib\site-packages\web\wsgiserver__init__.py", line 2058, in start_response
raise TypeError("WSGI response header value %r is not a byte string." % v)
TypeError: WSGI response header value u'text/css' is not a byte string.

@the5fire
Copy link
Owner

你的代码贴出来我看看

@fishenal
Copy link
Author

#coding:utf-8
import json

import web
from models import Todos
from web.httpserver import StaticMiddleware
urls = (
    '/', 'index',  #返回首页
    '/todo', 'todo',  #  处理POST请求
    '/todo/(\d*)', 'todo',  # 处理前端todo的请求,对指定记录进行操作
    '/todos/', 'todos',  # 处理前端todo的请求,返回所有数据
)

app = web.application(urls, globals())
application = app.wsgifunc(StaticMiddleware)

if web.config.get('_session') is None:
    session = web.session.Session(
        app,
        web.session.DiskStore('sessions'),
        initializer={'login': False, 'user': None}
    )
    web.config._session = session


render = web.template.render('')

# 首页
class index:
    def GET(self):
        # 渲染首页到浏览器
        return render.index()

class todo:
    def GET(self, todo_id=None):
        result = None
        itertodo = Todos.get_by_id(id=todo_id)
        for todo in itertodo:
            result = {
                "id": todo.id,
                "title": todo.title,
                "order": todo._order,
                "done": todo.done == 1,
            }
        return json.dumps(result)

    def POST(self):
        data = web.data()
        todo = json.loads(data)
        # 转换成_order, order是数据库关键字, sqlite3报错
        todo['_order'] = todo.pop('order')
        Todos.create(**todo)

    def PUT(self, todo_id=None):
        data = web.data()
        todo = json.loads(data)
        todo['_order'] = todo.pop('order')
        Todos.update(**todo)

    def DELETE(self, todo_id=None):
        Todos.delete(id=todo_id)


class todos:
    def GET(self):
        todos = []
        itertodos = Todos.get_all()
        for todo in itertodos:
            todos.append({
                "id": todo.id,
                "title": todo.title,
                "order": todo._order,
                "done": todo.done == 1,
            })
        return json.dumps(todos)

if __name__ == "__main__":
    app.run()

@fishenal
Copy link
Author

结构是
init_sqlite.py
models.py
.
.
.
static

  • xxx.png
  • xxx.css
  • xxx.js

根目录static下js都能访问,png和css访问不了报上面的错误,html也加载不成功,服务器500

@the5fire
Copy link
Owner

我试了下你的代码,mac上可以运行。你js可以访问到,css也应该可以。你注意模板位置

@vixiaoan
Copy link

我自己的程序,在ubuntu下运行没有问题,在win8下运行碰到同样的问题,把web\wsgiserver__init__.py
的2056-2059行屏蔽掉就好了。不知道会不会造成其他问题

    for k, v in headers:
        #if not isinstance(k, str):
        #    raise TypeError("WSGI response header key %r is not a byte string." % k)
        #if not isinstance(v, str):
        #    raise TypeError("WSGI response header value %r is not a byte string." % v)
        if k.lower() == 'content-length':
            self.remaining_bytes_out = int(v)
    self.req.outheaders.extend(headers)
    return self.write

@the5fire
Copy link
Owner

@vixiaoan 你把k和v print出来看看,在win上这俩不是str?

Repository owner deleted a comment from smcgive Feb 23, 2024
Repository owner deleted a comment from marynavoitenko Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@the5fire @fishenal @vixiaoan and others