English 中文(简体)
原型: 使用 URL 路由( 使用 w/ Backbone. Router ) 的最简单的 HTTP 服务器吗?
原标题:Prototyping: Simplest HTTP server with URL routing (to use w/ Backbone.Router)?

我们正在研究一个 Backbone.js 应用程序,而且我们可以通过输入 < code> python - m SimpleHTPServer 启动一个 HTTP 服务器这一事实非常出色。

我们希望能够将任何 URL (例如 localhost:800/path/to/ something ) 连接到我们的 index.html 上,以便我们可以用 HTML5 < code> backbone.Router 测试 < backbone. < Router 。 < push State

要做到这一点,最痛苦的方法是什么?

最佳回答

只要在 baseHTTPServer 中使用在 python 功能中构建的 python 功能即可

import BaseHTTPServer

class Handler( BaseHTTPServer.BaseHTTPRequestHandler ):
    def do_GET( self ):
        self.send_response(200)
        self.send_header(  Content-type ,  text/html  )
        self.end_headers()
        self.wfile.write( open( index.html ).read() )

httpd = BaseHTTPServer.HTTPServer( ( 127.0.0.1 , 8000), Handler )
httpd.serve_forever()
问题回答
  1. 下载并安装 ""http://cherrypy.org/"rel="nofollow" >CherryPy

  2. 创建以下 python 脚本( 命名为 alway_ index. py 或类似的东西), 并以您想要使用的实际文件路径替换 c: index. html

    import cherrypy
    
    class Root:
        def __init__(self, content):
            self.content = content
    
        def default(self, *args):
            return self.content
        default.exposed = True
    
    cherrypy.quickstart(Root(open( c:index.html ,  r ).read()))
    
  3. Run python <path oalways_index.py>
  4. Point your browser at http://localhost:8080 and no matter what url you request, you always get the same content.

使用 Python3 简单的 HTTP 服务器和 Linux 的完整解决方案 :

  1. create new file spa-server:
    sudo nano /usr/local/bin/spa-server
    
  2. paste contents below:
    #!/usr/bin/env python
    
    import os
    import sys
    from urllib.parse import urlparse
    from http.server import SimpleHTTPRequestHandler
    from http.server import HTTPServer
    
    
    class Handler(SimpleHTTPRequestHandler):
        def do_GET(self):
            url = urlparse(self.path)
            request_file_path = url.path.strip( / )
    
            if not os.path.exists(request_file_path):
                if os.path.exists( index.html ):
                    self.path =  index.html 
                elif os.path.exists( index.htm ):
                    self.path =  index.htm 
                else:
                    print(f Not found: {request_file_path} )
    
            return SimpleHTTPRequestHandler.do_GET(self)
    
    
    try:
        param = sys.argv[1]
    except IndexError:
        param = None
    
    host =  0.0.0.0 
    if param is None:
        port = 8000
    else:
        if  :  in param:
            host = param.split( : )[0]
            port = int(param.split( : )[1])
        else:
            port = int(param)
    
    
    httpd = HTTPServer((host, port), Handler)
    print(f Serving HTTP on {host}:{port} )
    print(f Looking for static assets in {os.getcwd()} )
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print( Bye bye )
        sys.exit(0)
    
  3. Make file runnable:
    sudo chmod +x /usr/local/bin/spa-server
    
  4. Run server
    spa-server
    # or
    spa-server 8080
    # or
    spa-server localhost:8080
    

如果静态文件不存在, 服务器将尝试回复 index. html index.htm





相关问题
Using SketchFlow to prototype MS Surface Applications

We re doing mockups/prototyping on a MS Surface device and I wonder if anyone has succeeded in using SketchFlow for this. The problem that I see is that the code generated by the tool uses normal WPF ...

Any Other Ideas for prototyping

I ve used Douglass Crockford s Object.beget, but augmented it slightly to: Object.spawn = function (o, spec) { var F = function () {}, that = {}, node = {}; F.prototype = o; that = new F(); ...

Prototyping selenium Xpath paths in Firefox

I m using selenium in my test suite, but since it s slow to set up, I d like to prototype my xpaths on live pages, rather than waiting for tests to run. Is there a good way to do this? Firebug s $x ...

Suggest tool for website structure prototyping [closed]

I am looking for some tool that would help me prototype basic website structure and logistics (or simply user interface). It should be extremely efficient in the matter of time needed to do simple ...

Prototyping neural networks

from your experience, which is the most effective approach to implement artificial neural networks prototypes? It is a lot of hype about R (free, but I didn t work with it) or Matlab (not free), ...

Prototyping Object in Javascript breaks jQuery?

I have added a simple .js file to my page that has some pretty mundane common-task sort of functions added to the Object and Array prototypes. Through trial and error, I ve figured out that adding ...

热门标签