Can't connect python module to local docker ors instance

Hello,

I’m having a hard time getting my local instance of openrouteservice to work with openrouteservice-py. I was able to install successfully the application via docker. So, if I fire up the browser and type in the following url:

http://localhost:8080/ors/routes?profile=driving-car&coordinates=12.18288363,45.689405109|12.03793961,45.775061570

I get the expected result.

Now, following the python module instructions:

import openrouteservice
coords = ((12.18288363,45.689405109),(12.03793961,45.775061570))
client = openrouteservice.Client(base_url='http://localhost:8080/ors')
routes = client.directions(coords)

I get this error:

ConnectionError: HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /ors/v2/directions/driving-car/json (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd0f11ef6a0>: Failed to establish a new connection: [Errno 111] Connection refused'))

I can see that the endpoint in the browser request (“/ors/routes”) is different from the one searched by the python api (“/ors/v2/directions/driving-car/json”).

So, if I try the latter endpoint this way:

curl -X POST 'localhost:8080/ors/v2/directions/driving-car/json' -H 'Content-Type: application/json; charset=utf-8' -d '{"coordinates":[[12.182883635445991,45.68940510906503],[12.037939617550144,45.77506157013294]], "instructions":"false"}'

I get this message:

curl: (7) Failed to connect to localhost port 8080: Connection refused

After a bit of fiddling, I was able to get the answer from the server with this workaround (no idea why it works anyway):

curl -X POST '0.0.0.0:8080/ors/v2/directions/driving-car/json' -H 'Content-Type: application/json; charset=utf-8' -d '{"coordinates":[[12.182883635445991,45.68940510906503],[12.037939617550144,45.77506157013294]], "instructions":"false"}'

but initializing the client in the python module correspondingly has no success:

client = openrouteservice.Client(base_url='0.0.0.0:8080/ors')
routes = client.directions(coords)

In fact, it throws this exception:

InvalidSchema: No connection adapters were found for '0.0.0.0:8080/ors/v2/directions/driving-car/json?'

Ok. But then I can see the warning in the examples:

# If you did change the ORS endpoints for some reason
# you'll have to pass url and required parameters explicitly

and so I tried the following:

routes = client.request(
  url='0.0.0.0:8080/ors',
  post_json={
      'coordinates': coords,
      'profile': 'driving-car',
      'format': 'geojson'
  })

but got the error:

/.pyenv/versions/3.8.2/envs/maps/lib/python3.8/site-packages/openrouteservice/client.py in _urlencode_params(params)
    328     # urlencode does not handle unicode strings in Python 2.
    329     # Firstly, normalize the values so they get encoded correctly.
--> 330     params = [(key, _normalize_for_urlencode(val)) for key, val in params]
    331     # Secondly, unquote unreserved chars which are incorrectly quoted
    332     # by urllib.urlencode, causing invalid auth signatures. See GH #72

TypeError: 'NoneType' object is not iterable

The same goes for url = ‘/0.0.0.0:8080/ors’ and url = ‘/0.0.0.0:8080/ors/v2/directions/driving-car/json’

Does anybody have a clue of what I am getting wrong?
Thank you

Hi @frnc
It seems there was a bug with a fix that was waiting for a release, so hopefully that will go out soon.

When it comes to your request

routes = client.request(
  url='0.0.0.0:8080/ors',
  post_json={
      'coordinates': coords,
      'profile': 'driving-car',
      'format': 'geojson'
  })

you need to do it a bit different. As you have already defined the “base” url for the client when you created it, the url you pass in the request is the information for the endpoint, so you would use:

routes = client.request(
  url='/v2/directions/driving-car/geojson',
  post_json={
      'coordinates': coords
  })

Hi @frnc,

Could you try all of the following combinations (some you already have):

localhost:8080/ors/v2/directions/driving-car,
http://localhost:8080/ors/v2/directions/driving-car,
0.0.0.0:8080/ors/v2/directions/driving-car,
http://0.0.0.0:8080/ors/v2/directions/driving-car,
127.0.0.1:8080/ors/v2/directions/driving-car,
http://127.0.0.1:8080/ors/v2/directions/driving-car

I think it’s to do with the http but not sure.
Also you might want to set some breakpoints in the openrouteservice-py code and see if you can check the response directly in the source code.

Best regards

The fix release has been made now, so if you upgrade the package in pip, you shouldn’t get the TypeError: 'NoneType' object is not iterable error anymore.

Hi all,
thank you for your feedback, and thanks very very much for the bug fix. I have tried the new release, and can confirm that the python TypeError: 'NoneType' object is not iterable error has gone. As for the combinations, here’s what I got:

'b_url': localhost:8080/ors, endpoint: /v2/directions/driving-car --> Nope ('No connection adapters were found for 'localhost:8080/ors/v2/directions/driving-car'')
'b_url': localhost:8080/ors, endpoint: /v2/directions/driving-car/json --> Nope ('No connection adapters were found for 'localhost:8080/ors/v2/directions/driving-car/json'')
'b_url': http://localhost:8080/ors, endpoint: /v2/directions/driving-car --> Nope ('HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /ors/v2/directions/driving-car (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f36a2f98ee0>: Failed to establish a new connection: [Errno 111] Connection refused'))')
'b_url': http://localhost:8080/ors, endpoint: /v2/directions/driving-car/json --> Nope ('HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /ors/v2/directions/driving-car/json (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f36a2fb8400>: Failed to establish a new connection: [Errno 111] Connection refused'))')
'b_url': 0.0.0.0:8080/ors, endpoint: /v2/directions/driving-car --> Nope ('No connection adapters were found for '0.0.0.0:8080/ors/v2/directions/driving-car'')
'b_url': 0.0.0.0:8080/ors, endpoint: /v2/directions/driving-car/json --> Nope ('No connection adapters were found for '0.0.0.0:8080/ors/v2/directions/driving-car/json'')
'b_url': http://0.0.0.0:8080, endpoint: /v2/directions/driving-car --> Nope ('HTTP Error: 404')
'b_url': http://0.0.0.0:8080, endpoint: /v2/directions/driving-car/json --> Nope ('HTTP Error: 404')
'b_url': 127.0.0.1:8080/ors, endpoint: /v2/directions/driving-car --> Nope ('No connection adapters were found for '127.0.0.1:8080/ors/v2/directions/driving-car'')
'b_url': 127.0.0.1:8080/ors, endpoint: /v2/directions/driving-car/json --> Nope ('No connection adapters were found for '127.0.0.1:8080/ors/v2/directions/driving-car/json'')
'b_url': http://127.0.0.1:8080/ors, endpoint: /v2/directions/driving-car --> Yeah
'b_url': http://127.0.0.1:8080/ors, endpoint: /v2/directions/driving-car/json --> Yeah

So, there are two occurrences that actually work. Hope it can be useful. Again, thank you for your prompt support.

@frnc,

fyi: the /v2/directions/driving-car defaults to json output with encoded polyline, so internally it is exactly the same as the /v2/directions/driving-car/json endpoint :wink: