网络服务器的瓶装时间非常慢

时间:2012-05-06 22:59:53

标签: python html python-2.7 bottle

所以我目前正在开发一个基本的小网站来运行我的网络。但是,我遇到了一些问题。当我运行服务器时,在运行服务器的计算机上,我可以非常快速地访问这些页面。但是,当我尝试在我的网络上的另一台计算机上访问同一页面时,它会非常缓慢地加载。是因为我使用开发。服务器而不是像Paste或Apache这样的东西? (另请注意,当我查看服务器计算机时,请求的日志在我在另一台计算机上的浏览器上请求后大约5-6秒内出现)

我的代码如下:

正在访问的网页:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="content-type"
 content="text/html; charset=ISO-8859-1">
  <title>index</title>
  <link type="text/css"
 href="cssfiles/mainpagecss.css"
 rel="stylesheet">
</head>
<body>
<table
 style="width: 100%; text-align: left; margin-left: auto; margin-right: auto;"
 border="0" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td>
      <h1><span class="headertext">
      <center>Network
Website</center>
      </span></h1>
      </td>
    </tr>
  </tbody>
</table>
%if name!='none':
    <p align="right">signed in as: {{name}}</p>
%else:
    pass
%end
<br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2"
 cellspacing="2">
  <tbody>
    <tr>
      <td>
      <table style="text-align: left; width: 100%;" border="0"
 cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
            <td style="width: 15%; vertical-align: top;">
            <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
              <tbody>
                <tr>
                  <td>Home<br>
                  <span class="important">Teamspeak Download</span><br>
                  <span class="important">Teamspeak Information</span></td>
                </tr>
              </tbody>
            </table>
            </td>
            <td style="vertical-align: top;">
            <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
              <tbody>
                <tr>
                  <td>
                  <h1><span style="font-weight: bold;">Network Website</span></h1>
To find all of the needed information relating to the network's social
capabilities, please refer to the links in the side bar.</td>
                </tr>
              </tbody>
            </table>
            </td>
          </tr>
        </tbody>
      </table>
      </td>
    </tr>
  </tbody>
</table>
</body>
</html>

的CSS:

  .headertext { color: rgb(51, 51, 51);
    }

  .bodytext {  }

  .important { font-weight: bold;
    }

服务器:

from Bottle import route,run,template,request,static_file



@route('/')
def home():
    return template('Templates/',name=request.environ.get('REMOTE_ADDR'))

run(host='Work-PC',port=9999,debug=True)

3 个答案:

答案 0 :(得分:9)

我知道我迟到了,但我遇到了同样的问题。默认的瓶子服务器(来自wsgiref.simple_server的WSGIRef)为每个GET,POST等执行反向DNS查找,因此它可以在Web日志中使用连接主机名而不是其IP地址。这可能会不必要地降低速度,即使你有一个快速的DNS响应器。 :(

只需对bottle.py进行一点点黑客攻击,就可以重载执行rDNS的基础方法BaseHTTPRequestHandler.address_string(),如下所示:

bottle.py

 class WSGIRefServer(ServerAdapter):
     def run(self, handler): # pragma: no cover
         from wsgiref.simple_server import make_server, WSGIRequestHandler
+        self.fast = True
+        self.quiet = False
+        if self.fast and self.quiet:  # disable logging and rDNS
+            class FastAndQuietHandler(WSGIRequestHandler):
+                def address_string(self): return self.client_address[0]
+                def log_request(*args, **kw): pass
+            self.options['handler_class'] = FastAndQuietHandler
+        elif self.fast:  # disable Reverse DNS Lookups -> faster service
+            class FastHandler(WSGIRequestHandler):
+                def address_string(self): return self.client_address[0]
+            self.options['handler_class'] = FastHandler
-        if self.quiet:
+        elif self.quiet:  # disable action entries to web-log
             class QuietHandler(WSGIRequestHandler):
                 def log_request(*args, **kw): pass
             self.options['handler_class'] = QuietHandler
         srv = make_server(self.host, self.port, handler, **self.options)
         srv.serve_forever()

我不喜欢修补源代码,但是直到上游接受此修补程序,这很好用 IF 你决定使用默认服务器。

信用:请参阅 - https://stackoverflow.com/a/6761844/538418

HTH

答案 1 :(得分:8)

在尝试从同一网络上的另一台PC测试Bottle应用时,我遇到了同样的延迟问题。

解决方案是使用更好的多线程服务器运行Bottle。 cherrypy为我工作。

  1. 安装cherrypy:

    easy_install cherrypy

  2. 使用cherrypy改变瓶子运行:

    运行(app,host ='0.0.0.0',port = 8080,debug = True,reloader = True,server ='cherrypy')

  3. 注意:easy_install附带分发:http://pypi.python.org/pypi/distribute

    祝你好运!

答案 2 :(得分:3)

此问题已在较新版本的瓶中修复。

请参阅此补丁:https://github.com/defnull/bottle/pull/529