리디렉션이 어떻게 생성되는지 잘 모르겠습니다 ... 아주 기본적인 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()
그것을 밖으로 시도 :>"FOLDER ID: 500x"
편집
는 HTTP GET /folder/?id=500x
이 코드는 내가 생각하는 원하는 것을 SimpleHTTPServer-stuff 전에 기본적으로 기본 요청 처리기를 구현하고 do_GET(), do_PUT(), do_POST() 등을 구현합니다.
내가 보통하는 일은 요청 문자열 (re를 사용하여), 패턴 일치를 구문 분석하고 요청 처리기를 찾을 수 있는지 확인한 다음 가능한 경우 정적 요청에 대한 요청으로 처리합니다.
당신은 당신이 요청이 파일 저장소를 일치하는 경우, 당신은 주변에 일치하는이 패턴을 플립해야 가능하다면 정적 콘텐츠를 제공하고, FIRST보고 싶어하지 않을 경우, 다음 핸들러 :
SimpleHTTPServer는 파일 만 제공합니다. 요청에서 매개 변수를 처리하기 위해 다른 것을 사용하십시오. –