Forums

connecting to mongodb - ValueError: sockettimeoutms must be an integer or float

Hi, I'm new to coding so this hopefully is simple. I've get a hacker account and am trying to connect to my mongodb. I was getting a timeout error and saw the help page saying I need to apply these parameters:

connectTimeoutMS=30000, socketTimeoutMS=None, socketKeepAlive=True, connect=False, maxPoolsize=1

so I updated my connection string (not sure if that is right, I couldn't figure out another way to do this? and am now using this to connect:

app = Flask(__name__)
DB_URI = "mongodb+srv://<myusername>:<mypassword@cluster0-xqife.gcp.mongodb.net/<mydatabase>? 
retryWrites=true&w=majority&connectTimeoutMS=30000&socketTimeoutMS=None&socketKeepAlive=True
&connect=False&maxPoolsize=1"
app.config["MONGODB_HOST"] = DB_URI
db = MongoEngine(app)

if __name__ == '__main__':
    app.run()
app.config.from_object(Config)

from application import routes

I now get the following error "ValueError: sockettimeoutms must be an integer or float" I've tried changing the none to 0 and but it doesn't help, any help would be greatly appreciated.

Which package do you use to connect to mongo db?

so even though my code is the same I am now getting:

2020-05-09 18:25:46,219: Error running WSGI application 2020-05-09 18:25:46,239: ValueError: The value of socketkeepalive must be 'true' or 'false' 2020-05-09 18:25:46,239: File "/var/www/dreid254_pythonanywhere_com_wsgi.py", line 16, in <module> 2020-05-09 18:25:46,239: from application import app as application # noqa 2020-05-09 18:25:46,239: 2020-05-09 18:25:46,239: File "/home/dreid254/inkhorn/application/init.py", line 8, in <module> 2020-05-09 18:25:46,240: db = MongoEngine(app) 2020-05-09 18:25:46,240: 2020-05-09 18:25:46,240: File "/home/dreid254/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask_mongoengine/init.py", line 107, in init 2020-05-09 18:25:46,240: self.init_app(app, config) 2020-05-09 18:25:46,240: 2020-05-09 18:25:46,240: File "/home/dreid254/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask_mongoengine/init.py", line 134, in init_app 2020-05-09 18:25:46,240: connections = create_connections(config) 2020-05-09 18:25:46,240: 2020-05-09 18:25:46,240: File "/home/dreid254/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask_mongoengine/connection.py", line 108, in create_connections 2020-05-09 18:25:46,240:
conn_settings = get_connection_settings(config) 2020-05-09 18:25:46,240: 2020-05-09 18:25:46,240: File "/home/dreid254/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask_mongoengine/connection.py", line 95, in get_connection_settings 2020-05-09 18:25:46,240:
return _sanitize_settings(config) 2020-05-09 18:25:46,241: 2020-05-09 18:25:46,241: File "/home/dreid254/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask_mongoengine/connection.py", line 41, in _sanitize_settings 2020-05-09 18:25:46,241: uri_dict = uri_parser.parse_uri(uri_to_check) 2020-05-09 18:25:46,241: 2020-05-09 18:25:46,241: File "/home/dreid254/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/pymongo/uri_parser.py", line 428, in parse_uri 2020-05-09 18:25:46,241:
options.update(split_options(opts, validate, warn, normalize)) 2020-05-09 18:25:46,241: 2020-05-09 18:25:46,241: File "/home/dreid254/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/pymongo/uri_parser.py", line 296, in split_options 2020-05-09 18:25:46,241: options = validate_options(options, warn) 2020-05-09 18:25:46,241: 2020-05-09 18:25:46,241: File "/home/dreid254/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/pymongo/uri_parser.py", line 259, in validate_options 2020-05-09 18:25:46,241: return get_validated_options(opts, warn) 2020-05-09 18:25:46,242: 2020-05-09 18:25:46,242: File "/home/dreid254/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/pymongo/common.py", line 753, in get_validated_options 2020-05-09 18:25:46,242: value = validator(opt, value) 2020-05-09 18:25:46,242: 2020-05-09 18:25:46,242: File "/home/dreid254/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/pymongo/common.py", line 166, in validate_boolean_or_string 2020-05-09 18:25:46,242:
raise ValueError("The value of %s must be " 2020-05-09 18:25:46,242: ********* 2020-05-09 18:25:46,242: If you're seeing an import error and don't know why, 2020-05-09 18:25:46,242: we have a dedicated help page to help you debug: 2020-05-09 18:25:46,242: https://help.pythonanywhere.com/pages/DebuggingImportError/

It looks like you are using flask-mongoengine, but the advice on the help page was for Flask-PyMongo. Maybe see into flask-mongoengine docs.

ok, thanks, I'll have a look

got it working using MongoEngine by altering my init file to include the same parameters specified for pymongo:

from flask import Flask
from config import Config
from flask_mongoengine import MongoEngine

app = Flask(__name__)
app.config['MONGODB_SETTINGS'] = {
    'host': 'mongodb+srv://<username>:<passwor>@cluster0-xqife.gcp.mongodb.net/<dbname>?retryWrites 
   =true&w=majority',
'connect': False,
'connectTimeoutMS': 30000,
'socketTimeoutMS': None,
'socketKeepAlive': True,
'maxPoolsize': 1
}

db = MongoEngine(app)

if __name__ == '__main__':
    app.run()
app.config.from_object(Config)

from application import routes

so really its the same parameters as pymongo, it was how to apply them as a beginner to coding that baffled me. I hope this helps omeone else who faces a similar problem some day.

Glad that you figured it out!

Hello,

I have the same issue. Instead of flask-pymongo, I am using flask-mongoengine. While I can connect to the Atlas using the above settings from my home computer.

Apparently the server is repeatedly trying to connect to the localhost only 27017. And I wonder why?

Edit1: I have a hacker account.

Edit2: Holly molly, I figured it out. I am using Flask-Session to store session on the server side as by default flask uses cookie based sessions. Restoring back to the cookie bases session is making it work. Now I need to check the docs of Flask-Session :)

Glad to see that you figured it out!