添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
handler = http.server.SimpleHTTPRequestHandler httpd = socketserver.TCPServer(address, handler) print(f"Serveur démarré sur le PORT {port}") httpd.serve_forever()

It's working fine. but i would :

  • Run a web sever exposing textplain content (and not Html content).
  • Set manually the workpath and name of index file (default: index.html)
  • keep Python server Code simple and light
  • I found some help on the web :

    handler.extensions_map['Content-type'] = 'text/plain'
    handler.send_header('Content-Type','text/plain')
    

    但是这些建议都没有用。 你能帮助我建立一个简单的Python代码来做这个吗?

    非常感谢。

    python
    hugo gutekunst
    hugo gutekunst
    发布于 2020-07-15
    2 个回答
    dejanualex
    dejanualex
    发布于 2020-07-15
    已采纳
    0 人赞同

    只使用内置模块的Python 2的脚本,只需将你想要提供的文件的绝对路径<INSERT_FILE>

    #!/usr/bin/python from SimpleHTTPServer import SimpleHTTPRequestHandler import BaseHTTPServer from io import StringIO import sys import os class MyHandler(SimpleHTTPRequestHandler): def send_head(self): # Place here the absolute path of the file with open("<INSERT_FILE>", "r") as f: body = unicode("".join( f.readlines())) self.send_response(200) self.send_header("Content-type", "text/html; charset=UTF-8") self.send_header("Content-Length", str(len(body))) #self.send_header("Server", "SimpleHTTP/1.1 Python/2.7.5") self.end_headers() # text I/O binary, and raw I/O binary # initial value must be unicode or None return StringIO(body) if __name__ == "__main__": HandlerClass = MyHandler ServerClass = BaseHTTPServer.HTTPServer Protocol = "HTTP/1.1" server_address = ('', 5555) HandlerClass.protocol_version = Protocol httpd = ServerClass (server_address, HandlerClass) print("serving on port 5555") httpd.serve_forever()

    对于python3(SimpleHTTPServer模块已经合并到http.server),放置绝对路径<INSERT_FILE>

    from http.server import HTTPServer, BaseHTTPRequestHandler class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.end_headers() # place absolute path here f_served = open('<INSERT_FILE>','rb') f_content = f_served.read() f_served.close() self.wfile.write(f_content) if __name__ == "__main__": httpd = HTTPServer(('localhost', 5555), SimpleHTTPRequestHandler) httpd.serve_forever()
    很好。用Python3来写会比较好。你能帮忙吗?
    更新,仅供参考,这不是最好的方法,只是一个POC。
    creolo
    creolo
    发布于 2020-07-15
    0 人赞同

    我建议使用aiohttp和它的低级别的服务器,它被描述为here:

    你可以返回纯文本,或者你把你的web.Response的内容类型改为text/html来发送将被解释为html的数据。

    你可以把text="OK"中的"OK"替换成你想要的任何纯文本。或者你用你的*.html的内容替换它,然后改变content_type。

    import asyncio
    from aiohttp import web
    async def handler(request):
        return web.Response(text="OK")
    async def main():
        server = web.Server(handler)
        runner = web.ServerRunner(server)
        await runner.setup()
        site = web.TCPSite(runner, 'localhost', 8080)
        await site.start()
        print("======= Serving on http://127.0.0.1:8080/ ======")
        # pause here for very long time by serving HTTP requests and
        # waiting for keyboard interruption
        await asyncio.sleep(100*3600)
    loop = asyncio.get_event_loop()