I use geopy to convet a coordinate into an address in order to get the name of the country and this is my code :
def convert_latandlong_to_address(self):
geolocator = Nominatim(user_agent="miz")
location = geolocator.reverse(self.lat+","+self.lng,timeout=10)
address = location.raw['address']
return address
it works fine but i realized that after several requests it returns 502 error :
geopy.exc.GeocoderServiceError: Non-successful status code 502
and this is full traceback:
Traceback (most recent call last):
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/geopy/geocoders/base.py", line 360, in _call_geocoder
result = self.adapter.get_json(url, timeout=timeout, headers=req_headers)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/geopy/adapters.py", line 373, in get_json
resp = self._request(url, timeout=timeout, headers=headers)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/geopy/adapters.py", line 401, in _request
raise AdapterHTTPError(
geopy.adapters.AdapterHTTPError: Non-successful status code 502
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/decorators.py", line 50, in handler
return func(*args, **kwargs)
File "/home/admin1/mizbanproject/location/preferlocation/api/views.py", line 15, in get_prefer_locations
address = location_obj.convert_latandlong_to_address()
File "/home/admin1/mizbanproject/location/preferlocation/api/utils.py", line 27, in convert_latandlong_to_address
location = geolocator.reverse(self.lat+","+self.lng,timeout=10)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/geopy/geocoders/nominatim.py", line 359, in reverse
return self._call_geocoder(url, callback, timeout=timeout)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/geopy/geocoders/base.py", line 378, in _call_geocoder
self._adapter_error_handler(error)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/geopy/geocoders/base.py", line 392, in _adapter_error_handler
raise exc_cls(str(error)) from error
geopy.exc.GeocoderServiceError: Non-successful status code 502
and after a few seconds or minutes when i run the code again it works i can't figure out what causes this problem and how can i prevent it ?