用Python实现一个简单的能够上传下载的HTTP服务器

您所在的位置:网站首页 26650锂电池什么牌子质量好 用Python实现一个简单的能够上传下载的HTTP服务器

用Python实现一个简单的能够上传下载的HTTP服务器

2024-07-17 11:41:29| 来源: 网络整理| 查看: 265

\n") for name in list: fullname = os.path.join(path, name) colorName = displayname = linkname = name # Append / for directories or @ for symbolic links if os.path.isdir(fullname): colorName = '' + name + '/' displayname = name linkname = name + "/" if os.path.islink(fullname): colorName = '' + name + '@' displayname = name # Note: a link to a directory displays with @ and links with / filename = os.getcwd() + '/' + displaypath + displayname f.write('%s%s%s\n' % (urllib.quote(linkname), colorName, sizeof_fmt(os.path.getsize(filename)), modification_date(filename))) f.write("\n\n\n\n") length = f.tell() f.seek(0) self.send_response(200) self.send_header("Content-type", "text/html") self.send_header("Content-Length", str(length)) self.end_headers() return f 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 = os.getcwd() 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 copyfile(self, source, outputfile): """Copy all data between two file objects. The SOURCE argument is a file object open for reading (or anything with a read() method) and the DESTINATION argument is a file object open for writing (or anything with a write() method). The only reason for overriding this would be to change the block size or perhaps to replace newlines by CRLF -- note however that this the default server uses this to copy binary data as well. """ shutil.copyfileobj(source, outputfile) def guess_type(self, path): """Guess the type of a file. Argument is a PATH (a filename). Return value is a string of the form type/subtype, usable for a MIME Content-type header. The default implementation looks the file's extension up in the table self.extensions_map, using application/octet-stream as a default; however it would be permissible (if slow) to look inside the data to make a better guess. """ base, ext = posixpath.splitext(path) if ext in self.extensions_map: return self.extensions_map[ext] ext = ext.lower() if ext in self.extensions_map: return self.extensions_map[ext] else: return self.extensions_map[''] if not mimetypes.inited: mimetypes.init() # try to read system mime.types extensions_map = mimetypes.types_map.copy() extensions_map.update({ '': 'application/octet-stream', # Default '.py': 'text/plain', '.c': 'text/plain', '.h': 'text/plain', }) class ThreadingServer(ThreadingMixIn, BaseHTTPServer.HTTPServer): pass def test(HandlerClass = SimpleHTTPRequestHandler, ServerClass = BaseHTTPServer.HTTPServer): BaseHTTPServer.test(HandlerClass, ServerClass) if __name__ == '__main__': # test() #单线程 # srvr = BaseHTTPServer.HTTPServer(serveraddr, SimpleHTTPRequestHandler) #多线程 srvr = ThreadingServer(serveraddr, SimpleHTTPRequestHandler) srvr.serve_forever()

REF:

1、httpserver ======================================= This httpserver is a enhanced version of SimpleHTTPServer. It was write in python, I use some code from bottle[https://github.com/defnull/bottle] It support resuming download, you can set the document root, it has more friendly error hit, and it can handle mimetype gracefully

https://github.com/lerry/httpserver/blob/master/httpserver.py

2、基于 java netty 的 SimpleHTTPServer,

由于windows不支持某些 netty low-level API,该代码仅能运行在 linux 下:

https://github.com/dvliman/SimpleHTTPServer



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭