pymysql,Lost connection to MySQL server during query - pymysql

Lost connection to MySQL server during query, how can I fix this? Better fix this in my program.
import pymysql
connection = pymysql.connect(host='***',
user='***',
password='***',
db='***',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
with connection.cursor() as cursor:
sql = "SELECT MAX(group_id) FROM topic_duplicate_check"
cursor.execute(sql) # Exception
r = cursor.fetchone()
max_gid = None
try:
max_gid = r['MAX(group_id)']
except:
pass
print(max_gid)
C:\ProgramData\Anaconda3\python.exe F:/group_topics/main.py
Traceback (most recent call last):
File "F:/group_topics/main.py", line 41, in
cursor.execute(sql)
File "C:\ProgramData\Anaconda3\lib\site-packages\pymysql\cursors.py", line 166, in execute
result = self._query(query)
File "C:\ProgramData\Anaconda3\lib\site-packages\pymysql\cursors.py", line 322, in _query
conn.query(q)
File "C:\ProgramData\Anaconda3\lib\site-packages\pymysql\connections.py", line 856, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\ProgramData\Anaconda3\lib\site-packages\pymysql\connections.py", line 1057, in _read_query_result
result.read()
File "C:\ProgramData\Anaconda3\lib\site-packages\pymysql\connections.py", line 1340, in read
first_packet = self.connection._read_packet()
File "C:\ProgramData\Anaconda3\lib\site-packages\pymysql\connections.py", line 987, in _read_packet
packet_header = self._read_bytes(4)
File "C:\ProgramData\Anaconda3\lib\site-packages\pymysql\connections.py", line 1033, in _read_bytes
CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
Process finished with exit code 1

Related

ServerSelectionTimeoutError( pymongo.errors.ServerSelectionTimeoutError: 147.234.32.246:27017

I try to connect remotely to mongodb server from pycharm(with RDP).
this is thefunction that i run:
cluster = MongoClient("mongodb://admin:Passw0rd!#147.234.32.246:27017/NEG")
db = cluster["NEG"]
for word in Setting.dictionary_global.keys():
if word in db.list_collection_names():
collection = db[word]
for file in Setting.dictionary_global[word].keys():
if collection.find({"url":Setting.dictionary_global[word][file].url}):
continue
num_of_appearance = len(Setting.dictionary_global[word][file].indexes.get(word))
post = {"url": file, "title": Setting.dictionary_global[word][file].title,
"description": Setting.dictionary_global[word][file].description,"word in page": Setting.dictionary_global[word][file].indexes,"appearance": num_of_appearance, "date modified": Setting.dictionary_global[word][file].time}
collection.insert_one(post)
else:
collection = db.create_collection(word)
for file in Setting.dictionary_global[word].keys():
#print(Setting.dictionary_global)
num_of_appearance = len(Setting.dictionary_global[word][file].indexes.get(word))
post = {"url": file, "title": Setting.dictionary_global[word][file].title,
"description": Setting.dictionary_global[word][file].description,"word in page": Setting.dictionary_global[word][file].indexes, "appearance": num_of_appearance, "date modified":Setting.dictionary_global[word][file].time}
collection.insert_one(post)
and i get this error:
'''
Traceback (most recent call last):
File "C:/Users/edend/PycharmProjects/pythonProject11/main.py", line 118, in
crawler.start()
File "C:/Users/edend/PycharmProjects/pythonProject11/main.py", line 110, in start
insertDB()
File "C:\Users\edend\PycharmProjects\pythonProject11\DB.py", line 10, in insertDB
if word in db.list_collection_names():
File "C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\database.py", line 863, in list_collection_names
for result in self.list_collections(session=session, **kwargs)]
File "C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\database.py", line 825, in list_collections
return self.__client._retryable_read(
File "C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\mongo_client.py", line 1460, in _retryable_read
server = self._select_server(
File "C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\mongo_client.py", line 1278, in _select_server
server = topology.select_server(server_selector)
File "C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\topology.py", line 241, in select_server
return random.choice(self.select_servers(selector,
File "C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\topology.py", line 199, in select_servers
server_descriptions = self._select_servers_loop(
File "C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\topology.py", line 215, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: 147.234.32.246:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 5ff3d15a2dcaa1e4fb3db4cd, topology_type: Single, servers: [<ServerDescription ('147.234.32.246', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('147.234.32.246:27017: timed out')>]>
'''
please help me im stuck and iv'e try everything
thank you in advance!!
Common causes:
MongoDB server is not running
MongoDB server is running on a different port
No connectivity between client and server (can you ping)
mongod.conf is configure to only allow local connections by default (set bind_ip_all?)

Error while restoring Couchbase database using cbrestore command in mac

I am getting the below error while restoring the couchbase database to my local mac from a server.
Traceback (most recent call last):
File "/Applications/Couchbase Server.app/Contents/Resources/couchbase-core/lib/python/cbrestore", line
12, in <module>
pump_transfer.exit_handler(pump_transfer.Restore().main(sys.argv))
File "/Applications/Couchbase Server.app/Contents/Resources/couchbase-core/lib/python/pump_transfer.py", line 80, in main
rv = pumpStation.run()
File "/Applications/Couchbase Server.app/Contents/Resources/couchbase-core/lib/python/pump.py", line 136, in run
rv = self.transfer_bucket_msgs(source_bucket, source_map, sink_map)
File "/Applications/Couchbase Server.app/Contents/Resources/couchbase-core/lib/python/pump.py", line 233, in transfer_bucket_msgs
source_map)
File "/Applications/Couchbase Server.app/Contents/Resources/couchbase-core/lib/python/pump_bfd.py", line 546, in total_msgs
rv, db, ver = connect_db(x, opts, CBB_VERSION)
ValueError: need more than 2 values to unpack
Any help is appreciated.
Thanks,
Emraan

How to set socks5 proxy in tweepy?

To ssh login my vps,the codes can run successfully.
import tweepy
consumer_key="xxx"
consumer_secret="xxx"
access_token="xxx"
access_token_secret="xxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)
for info in tweets:
print(info.text)
Set all software related with proxy, and test socks proxy.
url="https://www.youtube.com/watch?v=l9AC98amjSA"
youtube-dl --proxy socks5://127.0.0.1:1080 $url -o /tmp/downName
It is verified that the video on youtube can be downloaded with socks5 proxy.
All software related with proxy are in good status.
Now i want to set socks5 proxy in my local pc,only one line was changed based on the above codes.
api = tweepy.API(auth,proxy="127.0.0.1:1080")
The error info is as following.
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 588, in urlopen
self._prepare_proxy(conn)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 803, in _prepare_proxy
conn.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 291, in connect
self._tunnel()
File "/usr/lib/python3.5/http/client.py", line 827, in _tunnel
(version, code, message) = response._read_status()
File "/usr/lib/python3.5/http/client.py", line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.5/socket.py", line 576, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out
It is no use also to add socks proxy statement as following.
import urllib.request
proxy_support = urllib.request.ProxyHandler({'sock5': 'localhost:1080'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
import tweepy
consumer_key="xxx"
consumer_secret="xxx"
access_token="xxx"
access_token_secret="xxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth) # or api = tweepy.API(auth,proxy="127.0.0.1:1080")
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)
for info in tweets:
print(info.text)
How to set socks5 proxy in tweepy?
According to Tarun Lalwani,package installed and codes changed ,still don't work.
sudo pip3 install -U requests[socks]
Set all related proxy software.
sudo netstat -anlp |grep 1080
tcp 0 0 127.0.0.1:1080 0.0.0.0:* LISTEN 1465/python3
udp 0 0 127.0.0.1:1080 0.0.0.0:* 1465/python3
Then enter into python3 console.
import tweepy
consumer_key="xxx"
consumer_secret="xxx"
access_token="xxx"
access_token_secret="xxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,proxy="socks5://127.0.0.1:1080")
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)
An other error info occur.
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/urllib3/contrib/pyopenssl.py", line 441, in wrap_socket
cnx.do_handshake()
File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1716, in do_handshake
self._raise_ssl_error(self._ssl, result)
File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1431, in _raise_ssl_error
raise WantReadError()
OpenSSL.SSL.WantReadError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 346, in _make_request
self._validate_conn(conn)
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 850, in _validate_conn
conn.connect()
File "/usr/local/lib/python3.5/dist-packages/urllib3/connection.py", line 326, in connect
ssl_context=context)
File "/usr/local/lib/python3.5/dist-packages/urllib3/util/ssl_.py", line 329, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/usr/local/lib/python3.5/dist-packages/urllib3/contrib/pyopenssl.py", line 445, in wrap_socket
raise timeout('select timed out')
socket.timeout: select timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 440, in send
timeout=timeout
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 639, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/local/lib/python3.5/dist-packages/urllib3/util/retry.py", line 357, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/local/lib/python3.5/dist-packages/urllib3/packages/six.py", line 686, in reraise
raise value
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 349, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 309, in _raise_timeout
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: SOCKSHTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out. (read timeout=60)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tweepy/binder.py", line 187, in execute
proxies=self.api.proxy)
File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 521, in send
raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: SOCKSHTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out. (read timeout=60)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/tweepy/binder.py", line 245, in _call
return method.execute()
File "/usr/local/lib/python3.5/dist-packages/tweepy/binder.py", line 189, in execute
raise TweepError('Failed to send request: %s' % e)
tweepy.error.TweepError: Failed to send request: SOCKSHTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out. (read timeout=60)
Two things. One you need the requests[socks] package
pip install requests[socks]
Next you need to use the proxy in socks format
api = tweepy.API(auth,proxy="socks5://127.0.0.1:1080")

Odoo Internal Server Error while installing on amazon web server EC2

I am installing odoo8 on amazon EC2 web server . I installed ubuntu 14.04 on it and after that Odoo8 where executing all the steps it gives Internal Server Error for the Database Connection.
The error is,
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 177, in run_wsgi
execute(self.server.app)
File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 165, in execute
application_iter = app(environ, start_response)
File "/home/ubuntu/odoo/openerp/service/server.py", line 290, in app
return self.app(e, s)
File "/home/ubuntu/odoo/openerp/service/wsgi_server.py", line 216, in application
return application_unproxied(environ, start_response)
File "/home/ubuntu/odoo/openerp/service/wsgi_server.py", line 202, in application_unproxied
result = handler(environ, start_response)
File "/home/ubuntu/odoo/openerp/http.py", line 1290, in __call__
return self.dispatch(environ, start_response)
File "/home/ubuntu/odoo/openerp/http.py", line 1264, in __call__
return self.app(environ, start_wrapped)
File "/usr/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 579, in __call__
return self.app(environ, start_response)
File "/home/ubuntu/odoo/openerp/http.py", line 1406, in dispatch
self.setup_db(httprequest)
File "/home/ubuntu/odoo/openerp/http.py", line 1348, in setup_db
httprequest.session.db = db_monodb(httprequest)
File "/home/ubuntu/odoo/openerp/http.py", line 1478, in db_monodb
dbs = db_list(True, httprequest)
File "/home/ubuntu/odoo/openerp/http.py", line 1452, in db_list
dbs = dispatch_rpc("db", "list", [force])
File "/home/ubuntu/odoo/openerp/http.py", line 115, in dispatch_rpc
result = dispatch(method, params)
File "/home/ubuntu/odoo/openerp/service/db.py", line 73, in dispatch
return fn(*params)
File "/home/ubuntu/odoo/openerp/service/db.py", line 313, in exp_list
with closing(db.cursor()) as cr:
File "/home/ubuntu/odoo/openerp/sql_db.py", line 575, in cursor
return Cursor(self.__pool, self.dbname, self.dsn, serialized=serialized)
File "/home/ubuntu/odoo/openerp/sql_db.py", line 181, in __init__
self._cnx = pool.borrow(dsn)
File "/home/ubuntu/odoo/openerp/sql_db.py", line 464, in _locked
return fun(self, *args, **kwargs)
File "/home/ubuntu/odoo/openerp/sql_db.py", line 526, in borrow
result = psycopg2.connect(dsn=dsn, connection_factory=PsycoConnection)
File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
connection_factory=connection_factory, async=async)
OperationalError: FATAL: role "ubuntu" does not exist
2015-07-01 11:41:16,580 1315 ERROR None openerp.sql_db: Connection to the database failed
I think the problem is the postgres user. Check the db_user in the /etc/openerp-server.conf file. Its looks like the user is ubuntu which is not a postgres user. You need to create the user ubuntu in postgres or change the user in the config file to an existing postgres user.
The problem is user name ubuntu is not exists in your postgres database,
Let's move to solution:
First, You need to create use that has name ubuntu in your postgres using below command
CREATE USER ubuntu WITH PASSWORD 'ubuntu';
Then you need to give privilege to ubuntu user like this
ALTER USER ubuntu CREATEUSER CREATEDB;

werkzeug server failed to handle request

I'm trying to run this gevent server
from gevent import pywsgi
from Index import application
import paste.urlparser
import os
# http server: serves up static files
print "static files",os.path.dirname(__file__)
print "serving on localhost:8000"
http_server = pywsgi.WSGIServer(
('', 8000),
paste.urlparser.StaticURLParser(os.path.dirname(__file__)))
print 'Serving on https://127.0.0.1:4000'
server = pywsgi.WSGIServer(('0.0.0.0', 4000), application,spawn=None)
# Start the server greenlets
http_server.start()
# to start the server asynchronously, call server.start()
# we use blocking serve_forever() here because we have no other jobs
server.serve_forever()
I'm getting this exception where it gives me failed to handle request from werkzeug server.
I've used only werkzeug utility and have never used werkzeug server..
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gevent/server.py", line 131, in _do_accept
self._handle(client_socket, address)
File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 571, in handle
handler.handle()
File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 180, in handle
result = self.handle_one_request()
File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 287, in handle_one_request
raw_requestline = self.read_requestline()
File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 280, in read_requestline
return self.rfile.readline(MAX_REQUEST_LINE)
File "/usr/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
File "/usr/local/lib/python2.7/dist-packages/gevent/socket.py", line 432, in recv
wait_read(sock.fileno(), timeout=self.timeout, event=self._read_event)
File "/usr/local/lib/python2.7/dist-packages/gevent/socket.py", line 169, in wait_read
switch_result = get_hub().switch()
File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 154, in switch
assert cur is not self, 'Cannot switch to MAINLOOP from MAINLOOP'
AssertionError: Cannot switch to MAINLOOP from MAINLOOP
<WSGIServer fileno=4 address=0.0.0.0:4000>: Failed to handle request from ('127.0.0.1', 36088)

Resources