def process_item(self, item, spider):
if spider.name == 'RssCrawler':
# Search the CurrentVersion table for a version of the article
try:
self.cursor.execute(self.compare_versions, (item['url'],))
except (pymysql.err.OperationalError, pymysql.ProgrammingError, pymysql.InternalError,
pymysql.IntegrityError, TypeError) as error:
self.log.error("Something went wrong in rss query: %s", error)
# Save the result of the query. Must be done before the add,
# otherwise the result will be overwritten in the buffer
old_version = self.cursor.fetchone()
if old_version is not None:
# Compare the two download dates. index 3 of old_version
# corresponds to the download_date attribute in the DB
if (datetime.datetime.strptime(
item['download_date'], "%y-%m-%d %H:%M:%S") -
old_version[3]) \
< datetime.timedelta(hours=self.delta_time):
raise DropItem("Article in DB too recent. Not saving.")
return item
python类InternalError()的实例源码
def is_galera(self):
"""Check if local MySQL instance is a Galera cluster
:return: True if it's a Galera.
:rtype: bool
"""
try:
with self._cursor() as cursor:
cursor.execute("SELECT @@wsrep_on as wsrep_on")
row = cursor.fetchone()
return (str(row['wsrep_on']).lower() == "1" or
str(row['wsrep_on']).lower() == 'on')
except pymysql.InternalError as err:
error_code = err.args[0]
error_message = err.args[1]
if error_code == 1193:
LOG.debug('Galera is not supported or not enabled')
return False
else:
LOG.error(error_message)
raise
def kill_connection(self):
try:
self.Con.kill(self.Con.thread_id())
except (pymysql.OperationalError, pymysql.InternalError):
pass
def connect(self):
self.initConnection()
try:
self.connector = pymysql.connect(host=self.hostname, user=self.user, passwd=self.password, db=self.db, port=self.port, connect_timeout=conf.timeout, use_unicode=True)
except (pymysql.OperationalError, pymysql.InternalError, struct.error), msg:
raise SqlmapConnectionException(msg[1])
self.initCursor()
self.printConnected()
def execute(self, query):
retVal = False
try:
self.cursor.execute(query)
retVal = True
except (pymysql.OperationalError, pymysql.ProgrammingError), msg:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg[1])
except pymysql.InternalError, msg:
raise SqlmapConnectionException(msg[1])
self.connector.commit()
return retVal
def connect(self):
self.initConnection()
try:
self.connector = pymysql.connect(host=self.hostname, user=self.user, passwd=self.password, db=self.db, port=self.port, connect_timeout=conf.timeout, use_unicode=True)
except (pymysql.OperationalError, pymysql.InternalError), msg:
raise SqlmapConnectionException(msg[1])
self.initCursor()
self.printConnected()
def execute(self, query):
retVal = False
try:
self.cursor.execute(query)
retVal = True
except (pymysql.OperationalError, pymysql.ProgrammingError), msg:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg[1])
except pymysql.InternalError, msg:
raise SqlmapConnectionException(msg[1])
self.connector.commit()
return retVal
def connect(self):
self.initConnection()
try:
self.connector = pymysql.connect(host=self.hostname, user=self.user, passwd=self.password, db=self.db, port=self.port, connect_timeout=conf.timeout, use_unicode=True)
except (pymysql.OperationalError, pymysql.InternalError, struct.error), msg:
raise SqlmapConnectionException(msg[1])
self.initCursor()
self.printConnected()
def execute(self, query):
retVal = False
try:
self.cursor.execute(query)
retVal = True
except (pymysql.OperationalError, pymysql.ProgrammingError), msg:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg[1])
except pymysql.InternalError, msg:
raise SqlmapConnectionException(msg[1])
self.connector.commit()
return retVal
def update_message_sent_status(message, status):
message_id = message.get('message_id')
if not message_id:
return
mode = message.get('mode')
if not mode:
return
# Don't track this for twilio as those are kept track of separately. Make
# use of this for email, and, as a side effect of that for slack
if mode in ('sms', 'call'):
return
session = db.Session()
try:
session.execute('''INSERT INTO `generic_message_sent_status` (`message_id`, `status`)
VALUES (:message_id, :status)
ON DUPLICATE KEY UPDATE `status` = :status''',
{'message_id': message_id, 'status': status})
session.commit()
except (DataError, IntegrityError, InternalError):
logger.exception('Failed setting message sent status for message %s', message)
finally:
session.close()
def connect(self):
self.initConnection()
try:
self.connector = pymysql.connect(host=self.hostname, user=self.user, passwd=self.password, db=self.db, port=self.port, connect_timeout=conf.timeout, use_unicode=True)
except (pymysql.OperationalError, pymysql.InternalError), msg:
raise SqlmapConnectionException(msg[1])
self.initCursor()
self.printConnected()
def execute(self, query):
retVal = False
try:
self.cursor.execute(query)
retVal = True
except (pymysql.OperationalError, pymysql.ProgrammingError), msg:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg[1])
except pymysql.InternalError, msg:
raise SqlmapConnectionException(msg[1])
self.connector.commit()
return retVal
def reset_mysql(self):
"""
Resets the MySQL database.
"""
confirm = self.no_confirm
print("""
Cleanup MySQL database:
This will truncate all tables and reset the whole database.
""")
if not confirm:
confirm = 'yes' in builtins.input(
"""
Do you really want to do this? Write 'yes' to confirm: {yes}"""
.format(yes='yes' if confirm else ''))
if not confirm:
print("Did not type yes. Thus aborting.")
return
print("Resetting database...")
try:
# initialize DB connection
self.conn = pymysql.connect(host=self.mysql["host"],
port=self.mysql["port"],
db=self.mysql["db"],
user=self.mysql["username"],
passwd=self.mysql["password"])
self.cursor = self.conn.cursor()
self.cursor.execute("TRUNCATE TABLE CurrentVersions")
self.cursor.execute("TRUNCATE TABLE ArchiveVersions")
self.conn.close()
except (pymysql.err.OperationalError, pymysql.ProgrammingError, pymysql.InternalError,
pymysql.IntegrityError, TypeError) as error:
self.log.error("Database reset error: %s", error)
def execute(self, query, *multiparams, **params):
"""
???????????? ??? ??????? ???????? conn ? ????? ?????
"""
conn = params.pop('conn')
if conn is None:
raise MySQLNotConnectedException('MySQL not connected')
def coro_finished(f: asyncio.Future):
if f.cancelled():
self.logger.warning(
'execute of query is cancelled. q: %s [%s, %s]',
query, multiparams, params)
return
e = f.exception()
if e is not None and not isinstance(e,
pymysql.err.OperationalError):
self.logger.error(
'Exception happened while '
'executing query \'%s\': %s', query, str(e),
exc_info=e)
coro = asyncio.ensure_future(
conn.execute(query, *multiparams, **params),
loop=self.loop
)
coro.add_done_callback(coro_finished)
try:
res = await coro
return res
except asyncio.futures.TimeoutError as e:
raise MySQLTimeoutError('Timeout error') from e
except pymysql.InternalError as e:
err_code, err_msg = e.args
if err_code == MySQLError.DEADLOCK:
self.logger.error(
'Deadlock happened while executing query %s: %s',
query, e.args)
raise DeadlockError() from e
raise e