Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将值发送到BaseHTTPRequestHandler_Python - Fatal编程技术网

Python 将值发送到BaseHTTPRequestHandler

Python 将值发送到BaseHTTPRequestHandler,python,Python,我正在用Python中的HTTPServer和BaseHTTPRequestHandler创建一个简单的web服务器。以下是我到目前为止的情况: from handler import Handler #my BaseHTTPRequestHandler def run(self): httpd = HTTPServer(('', 7214), Handler) try: httpd.serve_forever() except KeyboardInte

我正在用Python中的HTTPServer和BaseHTTPRequestHandler创建一个简单的web服务器。以下是我到目前为止的情况:

from handler import Handler #my BaseHTTPRequestHandler

def run(self):
    httpd = HTTPServer(('', 7214), Handler)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()

我想为处理程序设置从中提供文件服务的基本路径,但我不确定如何做,因为它还没有被实例化?我觉得这真的很容易/很明显,但我想不出怎么做。我知道我可以在Handler类内部完成,但如果可能的话,我想在这里完成,因为我的所有配置都在这里阅读。

因为没有人想回答您的问题

只需将代码中的部分替换为注释“yourpath”


您想提供目录中的文件吗?默认情况下,SimpleHTTPServer已经为您完成了这项工作……我想我本可以使用
SimpleHTTPServer
/
SimpleHTTPRequestHandler
,但我认为我的问题对于这个类也是有效的。如何告诉SimpleHTTPRequestHandler使用特定目录而不仅仅是当前目录。我想我没有说得非常清楚。
python-msimplehttpserver
将根据需要提供来自CWD的文件。只是想让你的工作变得简单。:)
import os
import posixpath
import socket
import urllib
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler


class MyFileHandler(SimpleHTTPRequestHandler):
    def translate_path(self, path):
        """Translate a /-separated PATH to the local filename syntax.

        Components that mean special things to the local file system
        (e.g. drive or directory names) are ignored.  (XXX They should
        probably be diagnosed.)

        """
        # abandon query parameters
        path = path.split('?',1)[0]
        path = path.split('#',1)[0]
        path = posixpath.normpath(urllib.unquote(path))
        words = path.split('/')
        words = filter(None, words)
        path = '/' # yourpath
        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 run():
    try:
        httpd = HTTPServer(('', 7214), MyFileHandler)
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    except socket.error as e:
        print e
    else:
        httpd.server_close()