Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-3251

Make trailing whitespace visible in invalid port exception

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 4.2
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      When I connect using pymongo in localhost, this works:

       

      mongodb://mongo-service:27017

      But fails with this error when running on Kubernetes:

       

       

      ERROR:src.app:Exception on /api/polls [GET]
      Traceback (most recent call last):
        File "/.venv/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
          response = self.full_dispatch_request()
        File "/.venv/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
          rv = self.handle_user_exception(e)
        File "/.venv/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
          rv = self.dispatch_request()
        File "/.venv/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
          return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
        File "/home/appuser/src/app.py", line 28, in get_all
          polls = list(repository.get_all_polls())
        File "/home/appuser/src/repository.py", line 11, in get_all_polls
          return _get_collection().find()
        File "/home/appuser/src/repository.py", line 28, in _get_collection
          return mongo.get_collection(DATABASE, COLLECTION)
        File "/home/appuser/src/mongo.py", line 15, in get_collection
          return MongoClient(connection_string).get_database(database).get_collection(collection)
        File "/.venv/lib/python3.10/site-packages/pymongo/mongo_client.py", line 704, in __init__
          res = uri_parser.parse_uri(
        File "/.venv/lib/python3.10/site-packages/pymongo/uri_parser.py", line 568, in parse_uri
          nodes = split_hosts(hosts, default_port=default_port)
        File "/.venv/lib/python3.10/site-packages/pymongo/uri_parser.py", line 376, in split_hosts
          nodes.append(parse_host(entity, port))
        File "/.venv/lib/python3.10/site-packages/pymongo/uri_parser.py", line 137, in parse_host
          raise ValueError("Port must be an integer between 0 and 65535: %s" % (port,))
      ValueError: Port must be an integer between 0 and 65535: 27017 

      To make it work I had to add a slash at the end, which shouldn't be required as per the connection string specification.

       

       

      mongodb://mongo-service:27017/ 

      SO reference:

       

      https://stackoverflow.com/questions/72100777/valueerror-port-must-be-an-integer-between-0-and-65535-27017

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            evandro.pomatti@gmail.com Evandro Pomatti
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: