How to solve SMTPSenderRefused(code, resp, from_addr) error? - python-3.x

I am trying to send logs to email address using SMTPHandler in logging.But I got an error like below:
--- Logging error ---
Traceback (most recent call last):
File "/home/ab/anaconda3/envs/ei/lib/python3.9/logging/handlers.py", line 1065, in emit
smtp.send_message(msg)
File "/home/ab/anaconda3/envs/ei/lib/python3.9/smtplib.py", line 986, in send_message
return self.sendmail(from_addr, to_addrs, flatmsg, mail_options,
File "/home/ab/anaconda3/envs/ei/lib/python3.9/smtplib.py", line 887, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, b'5.7.0 Must issue a STARTTLS command first. b7-20020a170903228700b0017a0668befasm5585565plh.124 - gsmtp', 'None')
Call stack:
File "/home/ab/pa/par/processing.py", line 34, in <module>
logger.info("==========Script started to Run================")
Message: '==========Script started to Run================'
Arguments: ()
Code:
from environs import Env
import logging,logging.handlers
# Log file configuration setting Starts
MAIL_ID = os.environ.get("MAIL_ID")
MAIL_PASSWORD = os.environ.get("MAIL_PASSWORD")
cwd = os.getcwd()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
l_format = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s')
log_file = logging.FileHandler(cwd + "/cron_tab_log.log")
log_file.setFormatter(l_format)
logger.addHandler(log_file)
smtp_handler = logging.handlers.SMTPHandler(mailhost=('smtp.gmail.com',25),
fromaddr=MAIL_ID,
toaddrs=['ab#gmail.com', 'a#gmail.com'],
subject='Running Log',
credentials=(MAIL_ID,MAIL_PASSWORD),
secure=())
smtp_handler.setLevel(logging.DEBUG)
smtp_handler.setFormatter(l_format)
logger.addHandler(smtp_handler)
# Log file configuration setting Ends
Can anyone suggest a solution to solve this issue?

Related

Object has no attribute 'Client' error when trying to connect to BigQuery table in Google Cloud Function

I am trying to connect to a BigQuery table from a google cloud function but my function returns the following error:
Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging Details:
500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
When I inspect the logs, I see the following error:
Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/init.py", line 99, in view_func return function(request._get_current_object()) File "/workspace/main.py", line 72, in entry client = bigquery.Client() AttributeError: 'function' object has no attribute 'Client'
I don't understand why the attribute 'Client' doesn't exist. I believe I have imported bigquery correctly. Here is my entry point function with the simple test script:
import os
import json
from google.cloud import bigquery
def entry(request):
client = bigquery.Client()
query_job = client.query("""SELECT MAX(day) FROM `name of project and bigquery table`""")
for i in query_job:
print(i[0])
return "ok"
Here is my requirements.txt file
google-cloud-bigquery
sqlalchemy==1.4.37
pandas==1.4.2
PyMySQL==1.0.2
Any pointers would be greatly appreciated.
UPDATE
I had another function within my cloud function with was called bigquery. I think this caused a conflict with my import of bigquery in the third line of my code. I believe this is the root cause of my issue.

Why to request info logs generate in Quart but not Hypercorn?

I am trying to enable all requests to be logged (to centralized logging system) in a Quart microservice. However this only occurs when running directly in Python, and when running in Hypercorn it only logs major events and errors.
Running from PyCharm does generate the logs (to console, and to centralized log):
# TryLogging.py
import logging
from quart import Quart
app = Quart(__name__)
app.logger.setLevel(logging.INFO)
#app.route("/")
def callme():
return "I'm alive!"
#app.route("/fake_fail")
def failme():
raise Exception("Fake exception")
if __name__ == "__main__":
app.run()
generates console logs:
* Serving Quart app 'TryLogging'
* Environment: production
* Please use an ASGI server (e.g. Hypercorn) directly in production
* Debug mode: False
* Running on http://127.0.0.1:5000 (CTRL + C to quit)
[2022-01-10 15:55:48,323] Running on http://127.0.0.1:5000 (CTRL + C to quit)
[2022-01-10 15:55:50,080] 127.0.0.1:63560 GET / 1.1 200 10 4515
[2022-01-10 15:55:54,480] 127.0.0.1:63560 GET /fake_fail 1.1 500 290 1999
[2022-01-10 15:55:54,478] ERROR in app: Exception on request GET /fake_fail
Traceback (most recent call last):
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1489, in handle_request
return await self.full_dispatch_request(request_context)
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1514, in full_dispatch_request
result = await self.handle_user_exception(error)
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 964, in handle_user_exception
raise error
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1512, in full_dispatch_request
result = await self.dispatch_request(request_context)
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1557, in dispatch_request
return await self.ensure_async(handler)(**request_.view_args)
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\utils.py", line 66, in _wrapper
result = await loop.run_in_executor(
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\concurrent\futures\thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "C:\Users\brend\Documents\GitHub\ms-abs-boundaries\src\TryLogging.py", line 15, in failme
raise Exception("Fake exception")
Exception: Fake exception
However when running through Hypercorn in terminal (as it launched in production) and calling the endpoint from browser:
(ms-abs-boundaries) PS C:\Users\brend\Documents\GitHub\ms-abs-boundaries\src> hypercorn --bind 127.0.0.1:5008 TryLoggi
ng.py
[2022-01-10 15:56:42 +1100] [37772] [INFO] Running on http://127.0.0.1:5008 (CTRL + C to quit)
[2022-01-10 15:56:48,075] ERROR in app: Exception on request GET /fake_fail
Traceback (most recent call last):
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1489, in handle_request
return await self.full_dispatch_request(request_context)
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1514, in full_dispatch_
request
result = await self.handle_user_exception(error)
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 964, in handle_user_exc
eption
raise error
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1512, in full_dispatch_
request
result = await self.dispatch_request(request_context)
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1557, in dispatch_reque
st
return await self.ensure_async(handler)(**request_.view_args)
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\utils.py", line 66, in _wrapper
result = await loop.run_in_executor(
File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\concurrent\futures\thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "C:\Users\brend\Documents\GitHub\ms-abs-boundaries\src\TryLogging.py", line 15, in failme
raise Exception("Fake exception")
Exception: Fake exception
Only the exception is logged and the other success request is not logged.
How can I enable all requests (including other arbitrary info log events) to be logged when running in Hypercorn?
Hypercorn version: 0.13.2
Quart version: 0.16.2
NOTE: it needs to write to an external log system, not local logging file, and that external log is configured in the real version of the app. But getting it to show in the console is enough for testing.
Logs emitted by your application code would still be logged. What differs when you move from running with Quart to Hypercorn is that you no longer have the quart.serving logger which produced those messages you referred to.
To get similar behavior with Hypercorn, you can configure it to direct access log to stdout by setting accesslog to -.

xml-rpc returning protocol error 409 conflict

Using a python script for posting articles on WordPress website. That was working fine till few days. Now , its returning error related to 409 conflict.
Unable to track the reason as there is no change in code from my side.
Traceback (most recent call last):
File "/home/vikas/anaconda3/lib/python3.7/site-packages/wordpress_xmlrpc/base.py", line 24, in __init__
self.supported_methods = self.server.mt.supportedMethods()
File "/home/vikas/anaconda3/lib/python3.7/xmlrpc/client.py", line 1112, in __call__
return self.__send(self.__name, args)
File "/home/vikas/anaconda3/lib/python3.7/xmlrpc/client.py", line 1452, in __request
verbose=self.__verbose
File "/home/vikas/anaconda3/lib/python3.7/xmlrpc/client.py", line 1154, in request
return self.single_request(host, handler, request_body, verbose)
File "/home/vikas/anaconda3/lib/python3.7/xmlrpc/client.py", line 1187, in single_request
dict(resp.getheaders())
xmlrpc.client.ProtocolError: <ProtocolError for www.indiandefencenews.co.in/xmlrpc.php: 409 Conflict>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "t.py", line 16, in <module>
client = Client('https://www.indiandefencenews.co.in/xmlrpc.php','user','password')
File "/home/vikas/anaconda3/lib/python3.7/site-packages/wordpress_xmlrpc/base.py", line 27, in __init__
raise ServerConnectionError(repr(e))
wordpress_xmlrpc.exceptions.ServerConnectionError: <ProtocolError for www.indiandefencenews.co.in/xmlrpc.php: 409 Conflict>
My Code for the same is :
import urllib.request
import random
import csv
import os
import re
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
client = Client('https://www.website.com/xmlrpc.php','User','password')
# prepare metadata
data = {
'name': 'picture.jpg',
'type': 'image/jpeg', # mimetype
}
with open(filename, 'rb') as img:
data['bits'] = xmlrpc_client.Binary(img.read())
response = client.call(media.UploadFile(data))
attachment_id = response['id']
post = WordPressPost()
post.title = title
post.content = text
post.post_status = 'publish'
post.thumbnail = attachment_id
post.id = client.call(posts.NewPost(post))
PID=post.id
print ('Success')
strn=str(num)
url = url.replace(strn,'')
print (strn)
strn=int(num)+1
url=url+str(strn)
print (url)

discord.py os.environ doesn't work for bot token

so I have this code:
# bot.py
import os
import discord
from dotenv import load_dotenv
load_dotenv()
token = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(token)
The 'DISCORD_TOKEN' is stored in another file '.env' in the same directory which is:
#. env file
DISCORD_TOKEN={SSS}
Now whenever I try to run this code, I end up getting these erros:
Traceback (most recent call last):
File "C:\Users\HP\Python\lib\site-packages\discord\http.py", line 258, in static_login
data = await self.request(Route('GET', '/users/#me'))
File "C:\Users\HP\Python\lib\site-packages\discord\http.py", line 222, in request
raise HTTPException(r, data)
discord.errors.HTTPException: 401 UNAUTHORIZED (error code: 0): 401: Unauthorized
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "bot.py", line 18, in <module>
client.run(token)
File "C:\Users\HP\Python\lib\site-packages\discord\client.py", line 598, in run
return future.result()
File "C:\Users\HP\Python\lib\site-packages\discord\client.py", line 579, in runner
await self.start(*args, **kwargs)
File "C:\Users\HP\Python\lib\site-packages\discord\client.py", line 542, in start
await self.login(*args, bot=bot)
File "C:\Users\HP\Python\lib\site-packages\discord\client.py", line 400, in login
await self.http.static_login(token, bot=bot)
File "C:\Users\HP\Python\lib\site-packages\discord\http.py", line 262, in static_login
raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
But when I simply use
token = 'DSDS' instead of the .getenv, the code works and my bot is online.
Any fix to this?
Found out where I was wrong. I should have used '' instead of {} outside the token in my dotenv file.

Flask-SQLAlchemy ERROR: 'No application found. Either work inside a view function or push'

I am in the process of trying to use flask-sqlalchemy, and I have followed numerous examples in the documentation of how to get the Flask application setup. However, I am not able to get the app context for some reason. I am currently using the app.app_context().push() method. Is there some other way to get this to work? What am I doing wrong? Any help would be appreciated.
Currently running:
Python==3.6.3
Flask==0.12.2
Flask-Compress==1.4.0
Flask-SQLAlchemy==2.3.2
model.py
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship
db = SQLAlchemy()
class Property(db.Model):
__tablename__ = 'vt_property'
id = db.Column(db.BigInteger, primary_key=True)
name = db.Column(db.String(32))
value = db.Column(db.LargeBinary)
client_tag = db.Column(db.String(16))
index.py
from flask import Flask, jsonify, request
from flask_compress import Compress
from database.model import *
from manager.some_util import some_user, some_url, some_pass
# App config.
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
Compress(app)
app.secret_key = secret_key
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://connection_string'
db.init_app(app)
app.app_context().push()
some_util.py
from database.model import Property
from helpers.util import secret_key
def load_some_config():
from secure.secure import read_encrypted_text
some_configs = Property.query.filter(Property.name.contains('MANAGER')).all()
some_url = ''
some_user = ''
some_pass = ''
for c in some_configs:
if 'URL' in c.name:
some_url = c.value.decode('utf-8')
if 'USER' in c.name:
some_user = c.value.decode('utf-8')
if 'PASS' in c.name:
some_pass = read_encrypted_text(c.value).decode('utf-8')
return some_url, some_user, some_pass
When I try to run my API, I get this error:
*/venv/bin/python */src/api/index.py
Traceback (most recent call last):
File "*/venv/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 988, in __call__
return self.registry[key]
KeyError: <greenlet.greenlet object at 0xXXXXXXXX>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "*/src/api/index.py", line 16, in <module>
from manager.some_util import some_user, some_url, some_pass
File "*/src/manager/some_util.py", line 23, in <module>
some_url, some_user, some_pass = load_some_config()
File "*/src/manager/some_util.py", line 9, in load_some_config
some_configs = Property.query.filter(Property.name.contains(‘MANAGER’)).all()
File "*/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 514, in __get__
return type.query_class(mapper, session=self.sa.session())
File "*/venv/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 78, in __call__
return self.registry()
File "*/venv/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 990, in __call__
return self.registry.setdefault(key, self.createfunc())
File "*/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2882, in __call__
return self.class_(**local_kw)
File "*/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 141, in __init__
self.app = app = db.get_app()
File "*/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 912, in get_app
'No application found. Either work inside a view function or push'
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.
To execute some code within the Flask app context, the most simple way is doing this:
with self.app.app_context():
# ... your code here ...
Note that app is your Flask application object. The variable name might be different in your case.

Resources