gpt4 book ai didi

python - 如何在 Python 中获取文件的内容类型? (带网址..)

转载 作者:太空狗 更新时间:2023-10-29 22:07:19 27 4
gpt4 key购买 nike

假设我有一个视频文件:

http://mydomain.com/thevideofile.mp4

如何获取此文件的标题和内容类型?用 python 。但是,我不想下载整个文件。我希望它返回:

video/mp4

编辑:这就是我所做的。你怎么看?

f = urllib2.urlopen(url)
params['mime'] = f.headers['content-type']

最佳答案

像这样:

>>> import httplib
>>> conn = httplib.HTTPConnection("mydomain.com")
>>> conn.request("HEAD", "/thevideofile.mp4")
>>> res = conn.getresponse()
>>> print res.getheaders()

这只会下载并打印标题,因为它正在制作 HEAD要求:

Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

(通过 Wikipedia )

关于python - 如何在 Python 中获取文件的内容类型? (带网址..),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2143674/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com