コード例 #1
0
ファイル: core_server.py プロジェクト: richard-ma/playground
    def run(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind((self.address, self.port))
        sock.listen(5)

        connection, address = sock.accept()
        try:
            connection.settimeout(5)
            buf = connection.recv(1024)
            req = HttpRequest(buf)
            res = HttpResponse()

            if self._resource_exists(req.target) != False:
                target = self._resource_exists(req.target)
                if DEBUG:
                    print target
                with open(target, 'r') as f:
                    res.setBody(f.read())
                res.setStatus(200)
            else:
                res.setStatus(404)

            connection.send(res.__str__())
            if DEBUG:
                print req
                print res
        except socket.timeout:
            print 'timeout'
        finally:
            connection.close()

        sock.close()