Python 为什么SimpleHTTPServer重定向到?querystring/当我请求?querystring时?

Python 为什么SimpleHTTPServer重定向到?querystring/当我请求?querystring时?,python,simplehttpserver,webdev.webserver,Python,Simplehttpserver,Webdev.webserver,我喜欢使用Python的SimpleHTTPServer在本地开发各种需要通过Ajax调用加载资源的web应用程序 当我在URL中使用查询字符串时,服务器总是重定向到同一URL,并附加斜杠 例如,/folder/?id=1使用HTTP 301响应重定向到/folder/?id=1/ 我只需使用python-msimplehttpserver启动服务器 你知道我怎样才能摆脱重定向行为吗?这是Python 2.7.2。我不确定重定向是如何生成的。。。我已经尝试实现了一个非常基本的SimpleHTTP

我喜欢使用Python的SimpleHTTPServer在本地开发各种需要通过Ajax调用加载资源的web应用程序

当我在URL中使用查询字符串时,服务器总是重定向到同一URL,并附加斜杠

例如,
/folder/?id=1
使用HTTP 301响应重定向到
/folder/?id=1/

我只需使用
python-msimplehttpserver
启动服务器


你知道我怎样才能摆脱重定向行为吗?这是Python 2.7.2。

我不确定重定向是如何生成的。。。我已经尝试实现了一个非常基本的SimpleHTTPServer,在使用查询字符串参数时,我没有得到任何重定向

只需执行类似于
self.path.split(“/”)的操作,并在处理请求之前处理路径?
此代码符合您的要求我认为:

import SocketServer
import SimpleHTTPServer
import os


class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def folder(self):
        fid = self.uri[-1].split("?id=")[-1].rstrip()
        return "FOLDER ID: %s" % fid

    def get_static_content(self):
        # set default root to cwd
        root = os.getcwd()
        # look up routes and set root directory accordingly
        for pattern, rootdir in ROUTES:
            if path.startswith(pattern):
                # found match!
                path = path[len(pattern):]  # consume path up to pattern len
                root = rootdir
                break
        # normalize path and prepend root directory
        path = path.split('?',1)[0]
        path = path.split('#',1)[0]
        path = posixpath.normpath(urllib.unquote(path))
        words = path.split('/')
        words = filter(None, words)
        path = root
        for word in words:
            drive, word = os.path.splitdrive(word)
            head, word = os.path.split(word)
            if word in (os.curdir, os.pardir):
                continue
            path = os.path.join(path, word)
            return path

    def do_GET(self):
        path = self.path
        self.uri = path.split("/")[1:]

        actions = {
            "folder": self.folder,
        }
        resource = self.uri[0]
        if not resource:
            return self.get_static_content()
        action = actions.get(resource)
        if action:
            print "action from looking up '%s' is:" % resource, action
            return self.wfile.write(action())
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)


class MyTCPServer(SocketServer.ThreadingTCPServer):
    allow_reuse_address = True

httpd = MyTCPServer(('localhost', 8080), CustomHandler)
httpd.allow_reuse_address = True
print "serving at port", 8080
httpd.serve_forever()
试一试:

HTTP GET/folder/?id=500x
->“文件夹id:500x”

编辑:

好的,如果您以前没有使用过SimpleHTTPServer,那么您基本上实现了基本请求处理程序,实现do_GET()、do_PUT()、do_POST()等等

然后,我通常会解析请求字符串(使用re)、模式匹配,看看是否可以找到请求处理程序,如果找不到,则尽可能将请求作为静态内容的请求处理


如果可能的话,您说您希望提供静态内容,那么您应该翻转此模式匹配,首先查看请求是否与文件存储匹配,如果不匹配,然后与处理程序匹配:)

好的。在Morten的帮助下,我想出了这个,这似乎就是我所需要的:只要忽略查询字符串(如果它们存在)并为静态文件提供服务

import SimpleHTTPServer
import SocketServer

PORT = 8000


class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def __init__(self, req, client_addr, server):
        SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, req, client_addr, server)

    def do_GET(self):
        # cut off a query string
        if '?' in self.path:
            self.path = self.path.split('?')[0]
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)


class MyTCPServer(SocketServer.ThreadingTCPServer):
    allow_reuse_address = True

if __name__ == '__main__':
    httpd = MyTCPServer(('localhost', PORT), CustomHandler)
    httpd.allow_reuse_address = True
    print "Serving at port", PORT
    httpd.serve_forever()

要确保查询参数保持其应有状态,正确的方法是确保直接请求文件名,而不是让
SimpleHTTPServer
重定向到
index.html

例如
http://localhost:8000/?param1=1
执行重定向(301)并将url更改为
http://localhost:8000/?param=1/
这会弄乱查询参数

然而
http://localhost:8000/index.html?param1=1
(使索引文件显式)正确加载


因此,只要不让
SimpleHTTPServer
执行url重定向就可以解决问题。

SimpleHTTPServer只提供文件。使用其他方法处理请求中的参数。首先,谢谢!这表明可以防止重定向。不过,这个处理程序似乎不像我习惯的那样为静态文件提供服务。我基本上想要的是能够提供任何作为静态文件可用的路径,完全忽略查询字符串部分?它非常轻量级,只占上面代码的一小部分。我不会为此考虑web.py,因为据我所知,它默认只从/static/提供静态文件。因为我想做的就是提供静态文件,所以我没有费吹灰之力。你通常可以调整静态内容所在的位置等小因素,所以我不会仅仅通过这一点排除框架:)这似乎不尊重url参数。它仍然在查询参数后附加“/”。