backbonejs-learning-note icon indicating copy to clipboard operation
backbonejs-learning-note copied to clipboard

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

Open fishenal opened this issue 10 years ago • 6 comments

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.

fishenal avatar Jun 11 '14 09:06 fishenal

你的代码贴出来我看看

the5fire avatar Jun 11 '14 22:06 the5fire

#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 avatar Jun 12 '14 02:06 fishenal

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

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

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

fishenal avatar Jun 12 '14 02:06 fishenal

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

the5fire avatar Jun 13 '14 01:06 the5fire

我自己的程序,在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

vixiaoan avatar Jun 25 '14 07:06 vixiaoan

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

the5fire avatar Jun 26 '14 00:06 the5fire