使用Python请求从本地网址获取文件? [英] Fetch a file from a local url with Python requests?
问题描述
我在我的应用程序的一种方法中使用Python的请求库.该方法的主体如下所示:
I am using Python's requests library in one method of my application. The body of the method looks like this:
def handle_remote_file(url, **kwargs):
response = requests.get(url, ...)
buff = StringIO.StringIO()
buff.write(response.content)
...
return True
我想为该方法编写一些单元测试,但是,我想做的是传递一个伪造的本地URL,例如:
I'd like to write some unit tests for that method, however, what I want to do is to pass a fake local url such as:
class RemoteTest(TestCase):
def setUp(self):
self.url = 'file:///tmp/dummy.txt'
def test_handle_remote_file(self):
self.assertTrue(handle_remote_file(self.url))
当我使用本地网址调用 requests.get 时,出现了以下 KeyError 异常:
When I call requests.get with a local url, I got the KeyError exception below:
requests.get('file:///tmp/dummy.txt')
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/urllib3/poolmanager.pyc in connection_from_host(self, host, port, scheme)
76
77 # Make a fresh ConnectionPool of the desired type
78 pool_cls = pool_classes_by_scheme[scheme]
79 pool = pool_cls(host, port, **self.connection_pool_kw)
80
KeyError: 'file'
问题是如何将本地URL传递给 requests.get ?
The question is how can I pass a local url to requests.get?
PS:我整理了上面的示例.它可能包含许多错误.
PS: I made up the above example. It possibly contains many errors.
推荐答案
如@WooParadog所述,请求库不知道如何处理本地文件.尽管当前版本允许定义运输适配器.
As @WooParadog explained requests library doesn't know how to handle local files. Although, current version allows to define transport adapters.
因此,您只需定义自己的适配器即可处理本地文件,例如:
Therefore you can simply define you own adapter which will be able to handle local files, e.g.:
from requests_testadapter import Resp
class LocalFileAdapter(requests.adapters.HTTPAdapter):
def build_response_from_file(self, request):
file_path = request.url[7:]
with open(file_path, 'rb') as file:
buff = bytearray(os.path.getsize(file_path))
file.readinto(buff)
resp = Resp(buff)
r = self.build_response(request, resp)
return r
def send(self, request, stream=False, timeout=None,
verify=True, cert=None, proxies=None):
return self.build_response_from_file(request)
requests_session = requests.session()
requests_session.mount('file://', LocalFileAdapter())
requests_session.get('file://<some_local_path>')
在上面的示例中,我正在使用 requests-testadapter 模块.
I'm using requests-testadapter module in the above example.
这篇关于使用Python请求从本地网址获取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!