open file in python network url

Solutions on MaxInterview for open file in python network url by the best coders in the world

showing results for - "open file in python network url"
Valeria
16 Jun 2020
1from requests_testadapter import Resp
2
3class LocalFileAdapter(requests.adapters.HTTPAdapter):
4    def build_response_from_file(self, request):
5        file_path = request.url[7:]
6        with open(file_path, 'rb') as file:
7            buff = bytearray(os.path.getsize(file_path))
8            file.readinto(buff)
9            resp = Resp(buff)
10            r = self.build_response(request, resp)
11
12            return r
13
14    def send(self, request, stream=False, timeout=None,
15             verify=True, cert=None, proxies=None):
16
17        return self.build_response_from_file(request)
18
19requests_session = requests.session()
20requests_session.mount('file://', LocalFileAdapter())
21requests_session.get('file://<some_local_path>')