webapp.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:thunderstorm 作者: rnovacek 项目源码 文件源码
def process_input(self, s, cl, addr):
        print('client connected from', addr)
        cl.settimeout(None)

        if not hasattr(cl, 'readline'):
            # CPython
            client_stream = cl.makefile("rwb")
        else:
            # MicroPython
            client_stream = cl

        req = client_stream.readline()
        print(req)
        while True:
            data = client_stream.readline()
            if not data or data == b'\r\n':
                break

        method, path, protocol = req.split(b' ')
        if method.lower() == b'get':
            if path == b'/':
                self.send(cl, 200, filename='index.html', content_type='text/html; charset=utf-8')
            elif path == b'/commands':
                commands = ', '.join('"' + c + '"' for c in sorted(WebApp.commands))
                self.send(cl, 200, '[' + commands + ']', content_type='application/json')
            else:
                self.send(cl, 404)
        elif method.lower() == b'post':
            try:
                func = WebApp.commands[path.lstrip(b'/').decode('ascii')]
            except KeyError:
                self.send(cl, 404)
                return
            self.send(cl, 200)
            return func
        else:
            self.send(cl, 400)

        #mem = gc.mem_alloc()
        #gc.collect()
        #print("Freeing", mem - gc.mem_alloc(), "now free", gc.mem_free())
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号