I'm trying to learn how to work with MongoDB in Python but everytime I run my code it takes so long that I have to kill it and I get these tracebacks.
"/Volumes/ADATA HD720/flapPY/venv/bin/python" "/Volumes/ADATA HD720/flapPY/Database.py"
Traceback (most recent call last):
File "/Volumes/ADATA HD720/flapPY/Database.py", line 8, in <module>
x = high_scores.insert_one(mydict)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/collection.py", line 693, in insert_one
session=session),
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/collection.py", line 607, in _insert
bypass_doc_val, session)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/collection.py", line 595, in _insert_one
acknowledged, _insert_command, session)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1247, in _retryable_write
with self._tmp_session(session) as s:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1576, in _tmp_session
s = self._ensure_session(session)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1563, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1516, in __start_session
server_session = self._get_server_session()
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1549, in _get_server_session
return self._topology.get_server_session()
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/topology.py", line 427, in get_server_session
None)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/topology.py", line 208, in _select_servers_loop
self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 300, in wait
gotit = waiter.acquire(True, timeout)
KeyboardInterrupt
Process finished with exit code 1
This is my code:
import pymongo
client = pymongo.MongoClient("localhost", 27017)
database = client["database"]
high_scores = database["highscore"]
mydict = {"highscore" : 5}
x = high_scores.insert_one(mydict)
print(x.inserted_id)
My belief is that it cannot connect to the localhost
Apparently mongodb was not started on my PC. All I had to do was to run
brew services start mongodb
and it worked perfectly.
Related
I have a problem: I wrote python code to communicate with other people through a database that runs on Pymongo. My program was able to connect to the database but could not send files (this is how I tried to get my variable to the database: db.insert_one (msg)). The computer gives me this error:
Traceback (most recent call last):
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 1394, in _get_socket
sock_info = self.sockets.popleft()
IndexError: pop from an empty deque
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Luis\PycharmProjects\PresentationGuru2021\main.py", line 133, in <module>
Startup()
File "C:\Users\Luis\PycharmProjects\PresentationGuru2021\main.py", line 63, in Startup
main()
File "C:\Users\Luis\PycharmProjects\PresentationGuru2021\main.py", line 128, in main
db.insert_one(msg)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\collection.py", line 705, in insert_one
self._insert(document,
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\collection.py", line 620, in _insert
return self._insert_one(
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\collection.py", line 609, in _insert_one
self.__database.client._retryable_write(
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\mongo_client.py", line 1552, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\mongo_client.py", line 1438, in _retry_with_session
return self._retry_internal(retryable, func, session, bulk)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\mongo_client.py", line 1462, in _retry_internal
with self._get_socket(server, session) as sock_info:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\contextlib.py", line 117, in __enter__
return next(self.gen)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\mongo_client.py", line 1308, in _get_socket
with server.get_socket(
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\contextlib.py", line 117, in __enter__
return next(self.gen)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 1331, in get_socket
sock_info = self._get_socket(all_credentials)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 1397, in _get_socket
sock_info = self.connect(all_credentials)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 1297, in connect
sock_info.check_auth(all_credentials)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 820, in check_auth
self.authenticate(credentials)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 837, in authenticate
auth.authenticate(credentials, self)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\auth.py", line 672, in authenticate
auth_func(credentials, sock_info)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\auth.py", line 590, in _authenticate_default
return _authenticate_scram(credentials, sock_info, 'SCRAM-SHA-1')
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\auth.py", line 333, in _authenticate_scram
res = sock_info.command(source, cmd)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 710, in command
return command(self, dbname, spec, secondary_ok,
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\network.py", line 158, in command
helpers._check_command_response(
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\helpers.py", line 167, in _check_command_response
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: bad auth : Authentication failed., full error: {'ok': 0, 'errmsg': 'bad auth : Authentication failed.', 'code': 8000, 'codeName': 'AtlasError'}
Can someone help me fix this?
Here is the code (without my password from Pymongo):
cluster = MongoClient (
"mongodb + srv: // Luis: <NOPASSWORT HERE> # cluster0.mme3c.mongodb.net / myFirstDatabase? retryWrites = true & w = majority")
db = cluster ["socialMedia"] ["messaging"]
date = datetime.now (). strftime ("% x")
person = username
message = input ("Message:")
time = datetime.now (). strftime ("% X")
msg = {"User": person, "message": message, "Date": date, "Time": time}
print (msg)
db.insert_one (msg)
I am new to python and trying to use sqlalchemy statements for e.g read_sql_table.
I am using pycharm to execute below code.
When I am executing below code its not giving me any error :
import pandas as pd
import sqlalchemy
engine = sqlalchemy.create_engine('mysql+pymysql://root:*****#localhost:3306/STAGING')
print("BLAH")
OUTPUT:
BLAH
But when I am trying to execute below statement its giving me error
import pandas as pd
import sqlalchemy
engine = sqlalchemy.create_engine('mysql+pymysql://root:*****#localhost:3306/STAGING')
print("BLAH")
df = pd.read_sql_table("customers",engine)
ERROR:
BLAH Traceback (most recent call last): File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\pool.py", line 950, in _do_get
return self._pool.get(wait, self._timeout) File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\util\queue.py", line 145, in get
raise Empty sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:/Users/suksengupta/PycharmProjects/Project_July2019/DB.py", line 6, in <module>
df = pd.read_sql_table("customers",engine) File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\pandas\io\sql.py", line 243, in read_sql_table
meta.reflect(only=[table_name], views=True) File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 3311, in reflect
with bind.connect() as conn: File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1778, in connect
return self._connection_cls(self, **kwargs) File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\engine\base.py", line 60, in __init__
self.__connection = connection or engine.raw_connection() File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1847, in raw_connection
return self.pool.unique_connection() File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\pool.py", line 280, in unique_connection
return _ConnectionFairy._checkout(self) File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\pool.py", line 644, in _checkout
fairy = _ConnectionRecord.checkout(pool) File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\pool.py", line 440, in checkout
rec = pool._do_get() File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\pool.py", line 963, in _do_get
return self._create_connection() File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\pool.py", line 285, in _create_connection
return _ConnectionRecord(self) File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\pool.py", line 411, in __init__
self.connection = self.__connect() File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\pool.py", line 538, in __connect
connection = self.__pool._creator() File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\engine\strategies.py", line 90, in connect
return dialect.connect(*cargs, **cparams) File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\sqlalchemy\engine\default.py", line 377, in connect
return self.dbapi.connect(*cargs, **cparams) File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\pymysql\__init__.py", line 88, in Connect
return Connection(*args, **kwargs) File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\pymysql\connections.py", line 644, in __init__
self._connect() File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\pymysql\connections.py", line 837, in _connect
self._get_server_information() File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\pymysql\connections.py", line 1072, in _get_server_information
self.server_charset = charset_by_id(lang).name File "C:\Users\suksengupta\PycharmProjects\Project_July2019\venv\lib\site-packages\pymysql\charset.py", line 34, in by_id
return self._by_id[id] KeyError: 255
In output I am expecting all the data present under Customers table
Hi I'm using pyftpsync in order to upload new images (downloaded every day) in a online store skipping the existing ones.
From the author site I'm using this code (with my personal data of course):
from ftpsync.targets import FsTarget
from ftpsync.ftp_target import FtpTarget
from ftpsync.synchronizers import UploadSynchronizer
local = FsTarget("~/temp")
user ="joe"
passwd = "secret"
remote = FtpTarget("/temp", "example.com", username=user, password=passwd)
opts= {"force": False, "delete_unmatched": True, "verbose": 3, "dry_run" : False}
s = UploadSynchronizer(local, remote, opts,tls=True)
s.run()
This work for me correctly only if I delete the tls option, otherwise I obtain this error while connecting to my server:
Traceback (most recent call last):
File "sync.py", line 25, in <module>
s.run()
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/synchronizers.py", line 139, in run
res = self._sync_dir()
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/synchronizers.py", line 416, in _sync_dir
remote_entries = self.remote.get_dir()
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/ftp_target.py", line 310, in get_dir
self.ftp.retrlines("MLSD", _addline)
File "/usr/lib/python3.4/ftplib.py", line 466, in retrlines
resp = self.sendcmd('TYPE A')
File "/usr/lib/python3.4/ftplib.py", line 272, in sendcmd
return self.getresp()
File "/usr/lib/python3.4/ftplib.py", line 235, in getresp
resp = self.getmultiline()
File "/usr/lib/python3.4/ftplib.py", line 221, in getmultiline
line = self.getline()
File "/usr/lib/python3.4/ftplib.py", line 209, in getline
raise EOFError
EOFError
Could not remove lock file: Underlying socket connection gone (_ssl.c:1570)
Exception ignored in: <bound method FtpTarget.__del__ of <ftpsync.ftp_target.FtpTarget object at 0x7fe397ae7710>>
Traceback (most recent call last):
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/targets.py", line 70, in __del__
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/ftp_target.py", line 153, in close
File "/media/luca/1E9C3E629C3E349D/luca/Documenti/attività/Cataloghi2/ftpsync/ftp_target.py", line 187, in _unlock
File "/usr/lib/python3.4/ftplib.py", line 613, in delete
File "/usr/lib/python3.4/ftplib.py", line 271, in sendcmd
File "/usr/lib/python3.4/ftplib.py", line 198, in putcmd
File "/usr/lib/python3.4/ftplib.py", line 193, in putline
File "/usr/lib/python3.4/ssl.py", line 720, in sendall
File "/usr/lib/python3.4/ssl.py", line 681, in send
ssl.SSLError: Underlying socket connection gone (_ssl.c:1570)
It's obviously a problem related to the encryption, but I don't know how to fix it.
I need to create a client/server example with TLS 1.2 in Python. I spent many hours and I did not get anything...
I'm trying this example: https://github.com/titeuf87/python3-tls-example
I'm getting this error when I execute the client.py:
Exception in callback None
handle: <Handle cancelled>
Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\events.py", line 125, in _run
self._callback(*self._args)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\selector_events.py", line 667, in _read_ready
self._protocol.data_received(data)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\sslproto.py", line 486, in data_received
ssldata, appdata = self._sslpipe.feed_ssldata(data)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\sslproto.py", line 200, in feed_ssldata
self._sslobj.do_handshake()
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\ssl.py", line 633, in do_handshake
match_hostname(self.getpeercert(), self.server_hostname)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\ssl.py", line 300, in match_hostname
% (hostname, dnsnames[0]))
ssl.CertificateError: hostname '127.0.0.1' doesn't match 'server'
Traceback (most recent call last):
File "C:\Users\PC\workspace\testingpython\client.py", line 18, in <module>
loop.run_until_complete(connect())
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\base_events.py", line 342, in run_until_complete
return future.result()
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\futures.py", line 274, in result
raise self._exception
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\tasks.py", line 237, in _step
result = coro.throw(exc)
File "C:\Users\PC\workspace\testingpython\client.py", line 11, in connect
reader, writer = yield from asyncio.open_connection("127.0.0.1", 1234, ssl=sslcontext)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\streams.py", line 64, in open_connection
lambda: protocol, host, port, **kwds)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\base_events.py", line 669, in create_connection
sock, protocol_factory, ssl, server_hostname)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\base_events.py", line 692, in _create_connection_transport
yield from waiter
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\futures.py", line 385, in __iter__
yield self # This tells Task to wait for completion.
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\tasks.py", line 288, in _wakeup
value = future.result()
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\futures.py", line 274, in result
raise self._exception
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\events.py", line 125, in _run
self._callback(*self._args)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\selector_events.py", line 667, in _read_ready
self._protocol.data_received(data)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\sslproto.py", line 486, in data_received
ssldata, appdata = self._sslpipe.feed_ssldata(data)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\asyncio\sslproto.py", line 200, in feed_ssldata
self._sslobj.do_handshake()
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\ssl.py", line 633, in do_handshake
match_hostname(self.getpeercert(), self.server_hostname)
File "C:\Users\PC\AppData\Local\Programs\Python\Python35\lib\ssl.py", line 300, in match_hostname
% (hostname, dnsnames[0]))
ssl.CertificateError: hostname '127.0.0.1' doesn't match 'server'
Does anyone know of any examples?
I'm using python2.7 and fabric to connect to an sftp server.
Here's what I'm doing:
from fabric.api import env, get, run
env.key_filename = '/path/to/keyfile'
env.host_string = 'host.string'
env.user = 'user'
At this point, get('filename') successfully downloads a file from the sftp server. But run('ls') gives me:
[host.string] run: ls
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/fabric/network.py", line 647, in host_prompting_wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/fabric/operations.py", line 1054, in run
shell_escape=shell_escape)
File "/usr/local/lib/python2.7/dist-packages/fabric/operations.py", line 923, in _run_command
stderr=stderr, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/fabric/operations.py", line 764, in _execute
channel.get_pty(width=cols, height=rows)
File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 60, in _check
return func(self, *args, **kwds)
File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 177, in get_pty
self._wait_for_event()
File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 1086, in _wait_for_event
raise e
paramiko.ssh_exception.SSHException: Channel closed.
When I manually log into the sftp server, using the same creds, I can ls just fine.
Any ideas? Thanks.