Python httplib2 访问被拒绝

0 投票
1 回答
774 浏览
提问于 2025-04-17 18:26

我在运行一段简单代码时遇到了问题。

import httplib2
h = httplib2.Http(".cache")
resp, content = h.request("http://example.org/", "GET")

输出结果是

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1175, in __init__
    self.cache = FileCache(cache)
  File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 700, in __init__
    os.makedirs(self.cache)
  File "C:\Python27\lib\os.py", line 157, in makedirs
    mkdir(name, mode)
WindowsError: [Error 5] Access is denied: '.cache'

有没有人能给我一些建议,帮我解决这个错误?

1 个回答

1

要让这个工作正常,你可以直接不使用缓存目录:

import httplib2
h = httplib2.Http()
resp, content = h.request("http://example.org/", "GET")

正如上面的评论所说,你没有权限在你运行代码的路径下创建 .cache 目录。

当然,有一个缓存会更好……

如果你想要一个简单的、在内存中的缓存,这样就不会遇到权限问题,可以看看我写的这篇文章:http://grahamlyons.com/article/a-simple-in-memory-cache-for-python-s-httplib2

撰写回答