一种工具,可以轻松地找到要返回的适当mimetype
mimetype-match的Python项目详细描述
一个工具,用于解析接受头并找到适当的mimetype 轻松返回。只需传递accept头和服务器可以使用的mimetype 回来,它会找到最好的匹配。
这个包既有编程接口,也有 命令行界面。
安装
使用pip可以很容易地安装它
pip install mimetype-match
或者如果您希望克隆repo并安装它,只需运行以下命令 从包的根目录。
python setup.py develop
完整文档
使用命令行界面
mimetype-match [accept-header][server-mimetype1][server-mimetype2] ...
第一个参数应以逗号分隔 mimetypes作为适当格式的接受头。 附加的参数是来自服务器的mimetype 你想匹配的。例如,您可能有一个accept头 下列内容: text/*;q=0.3, text/html;q=0.7, text/html;level=1, /html;level=2;q=0.4, */*;q=0.5 服务器可以提供以下内容类型: ["text/html", "application/json", "audio/basic"]
在这种情况下,我们将运行以下命令:
mimetype-match "text/*;q=0.3, text/html;q=0.7, text/html;level=1, /html;level=2;q=0.4, */*;q=0.5""text/html""application/json""audio/basic"
这个命令会告诉您“text/html”是最好的选择。
使用软件包
以编程方式使用包与cli非常相似。
frommimetype_matchimportAcceptHeaderavailable_types=["text/html","application/json","audio/basic"]accept_header=AcceptHeader("text/*;q=0.3, text/html;q=0.7, text/html;level=1, /html;level=2;q=0.4, */*;q=0.5")best_match=accept_header.get_best_match(available_types)# It returns a tuple. The first object is a MimeType object# which has the orinal requested type from the client# The second object is the available type from the# server that best matches the clients requested types.print(best_match[0].mimetype)print(best_match[0].weight)print(best_match[1])
版本控制
在版本1.0.0之前,版本控制遵循sentimental versioning。在1.0.0 ollow之后发布 标准的major.minor.patch样式。
- 修补程序:前后兼容
- 小调:向后兼容
- 专业:无担保
贡献
想帮忙吗?我们会喜欢的!github将是mimetype匹配的开发中心。 如果您有任何问题、意见或投诉,请将其张贴在那里。另外,我们 肯定会接受拉取请求(提示:我们几乎总是喜欢更多的测试和 文件)。我们只有几个请求:
- 每个方法、函数和类都应该有一个完整的docstring
- 每个函数和方法至少应有一个单元测试
- 将请求保留到一个问题。(最好先在github上打开一个问题进行记录)