Why does SimpleHTTPServer redirect to ?querystring

2020-08-11 04:17发布

I like to use Python's SimpleHTTPServer for local development of all kinds of web applications which require loading resources via Ajax calls etc.

When I use query strings in my URLs, the server always redirects to the same URL with a slash appended.

For example /folder/?id=1 redirects to /folder/?id=1/ using a HTTP 301 response.

I simply start the server using python -m SimpleHTTPServer.

Any idea how I could get rid of the redirecting behaviour? This is Python 2.7.2.

3条回答
萌系小妹纸
2楼-- · 2020-08-11 04:28

Okay. With the help of Morten I've come up with this, which seems to be all I need: Simply ignoring the query strings if they are there and serving the static files.

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()
查看更多
虎瘦雄心在
3楼-- · 2020-08-11 04:30

The right way to do this, to ensure that the query parameters remain as they should, is to make sure you do a request to the filename directly instead of letting SimpleHTTPServer redirect to your index.html

For example http://localhost:8000/?param1=1 does a redirect (301) and changes the url to http://localhost:8000/?param=1/ which messes with the query parameter.

However http://localhost:8000/index.html?param1=1 (making the index file explicit) loads correctly.

So just not letting SimpleHTTPServer do a url redirection solves the problem.

查看更多
太酷不给撩
4楼-- · 2020-08-11 04:45

I'm not sure how the redirect is generated... I've tried implementing a very basic SimpleHTTPServer, and I don't get any redirects when using query string params.

Just do something like self.path.split("/") and process the path before handling the request? This code does what you want I think:

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()

Try it out:

HTTP GET /folder/?id=500x -> "FOLDER ID: 500x"

EDIT:

Okay so if you haven't used the SimpleHTTPServer-stuff before, you basically implement the base request handler, implement do_GET(), do_PUT(), do_POST() etc.

What I usually do then is parse the request string (using re), pattern match and see if I can find a request-handler and if not, handle the request as a request for static content if possible.

You say you want to serve static content if at all possible, then you should flip this pattern matching around, and FIRST see if the request matches the file-store and if not, then match against handlers :)

查看更多
登录 后发表回答