Browse Source

server static pages with a lighter way

 * This method does not eat your CPU
Oz N Tiram 9 years ago
parent
commit
cbb4452c53
2 changed files with 12 additions and 15 deletions
  1. 10 13
      blogit2.py
  2. 2 2
      conf.py

+ 10 - 13
blogit2.py

@@ -48,7 +48,9 @@ import subprocess as sp
 import SimpleHTTPServer
 import BaseHTTPServer
 import socket
+import SocketServer
 import thread
+
 try:
     import yaml  # in debian python-yaml
     from jinja2 import Environment, FileSystemLoader  # in debian python-jinja2
@@ -550,27 +552,22 @@ class StoppableHTTPServer(BaseHTTPServer.HTTPServer):
             self.handle_request()
 
 
-def preview(PREVIEW_ADDR='127.0.1.1', PREVIEW_PORT=11000):
+def preview():
     """
     launch an HTTP to preview the website
     """
+    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
+    SocketServer.TCPServer.allow_reuse_address = True
+    port = CONFIG['http_port']
+    httpd = SocketServer.TCPServer(("", port), Handler)
     os.chdir(CONFIG['output_to'])
     print "and ready to test at http://127.0.0.1:%d" % CONFIG['http_port']
     print "Hit Ctrl+C to exit"
     try:
-        httpd = StoppableHTTPServer(("127.0.0.1", CONFIG['http_port']),
-                                    SimpleHTTPServer.SimpleHTTPRequestHandler)
-        thread.start_new_thread(httpd.serve, ())
-        sp.call('xdg-open http://127.0.0.1:%d' % CONFIG['http_port'],
-                shell=True)
-        while True:
-            continue
-
+        httpd.serve_forever()
     except KeyboardInterrupt:
-        print
-        print "Shutting Down... Bye!."
-        print
-        httpd.stop()
+        httpd.shutdown()
+
 
 
 def publish(GITDIRECTORY=CONFIG['output_to']):

+ 2 - 2
conf.py

@@ -23,10 +23,10 @@ CONFIG = {
     'content_encoding': 'utf-8',
     'author': 'Oz Nahum Tiram',
     'editor': 'editor'
-}
+    }
 
 
-DB = {'posts': db.table('posts'), 'tags': db.table('tags'), 
+DB = {'posts': db.table('posts'), 'tags': db.table('tags'),
       'pages': db.table('pages'), 'templates': db.table('templates') }