I'm having trouble with how would I get the file chosen from forms to be sent in the email attachment. I am a beginner with this so I'm not familiar with how I could properly translate the input to the directory.
I do not how to properly translate this code to be able to get the uploaded file of the user:
file_attachments = [r.\somedirectory\some.jpg]
This is my code on trying to get the users uploaded file:
if request.method == "POST":
file_attachments = os.path.realpath(request.files['attfile']),
emailMsg = request.form['body']
mimeMessage= MIMEMultipart()
mimeMessage['to']=request.form['email']
mimeMessage['subject']=request.form['subject']
mimeMessage.attach(MIMEText(emailMsg,'plain'))
for attachment in file_attachments:
content_type, encoding= mimetypes.guess_type(attachment)
main_type,sub_type = content_type.split('/',1)
file_name = os.path.basename(attachment)
f = open(attachment,'rb')
myFile = MIMEBase(main_type, sub_type)
myFile.set_payload(f.read())
myFile.add_header('Content-Disposition', 'attachment', filename=file_name)
encoders.encode_base64(myFile)
f.close()
mimeMessage.attach(myFile)
raw_string = base64.urlsafe_b64encode(mimeMessage.as_bytes()).decode()
message = service.users().messages().send(
userId='me',
body = {'raw':raw_string}).execute()
return render_template('email.html')
The error message:
Traceback (most recent call last)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\Users\Shandon\Desktop\50\Capstone\website\auth.py", line 295, in send_message
Open an interactive python shell in this framefile_attachments = os.path.realpath(request.files['attfile']),
File "C:\Users\Shandon\Anaconda3\lib\ntpath.py", line 526, in abspath
return normpath(_getfullpathname(path))
Related
I am using flask to handle image uploads and whenever I upload a jpg, jpeg, or png the program I made is able to handle the upload. However, whenever uploading a .jfif image the program returns the error flask_uploads.UploadNotAllowed. Thanks for any help in advance!
The code the program is having an issue with is:
file_name = photos.save(request.files['photo'])
full traceback:
Traceback (most recent call last):
File
"C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\Users\user\Desktop\OCR - Copy\OCRWebsite\app.py", line 421, in upload
file_name = photos.save(request.files['photo'])
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_uploads.py", line 416, in save
raise UploadNotAllowed()
flask_uploads.UploadNotAllowed
You are probably using the standard IMAGES set:
https://github.com/jugmac00/flask-reuploaded/blob/f05077b085393dbc607c01b8daff1b3a8b2dbf0b/src/flask_uploads/extensions.py#L29
This set does not allow .jfif files.
However, such a set is really only a Python set, so you can create and use your own, or just update the existing one.
IMAGES.update(".jfif")
I'm trying to call Bitbucket API in my python script to retrieve some data.
I use the key/secret pair of Bitbucket OAuth.
data = { 'grant_type': 'client_credentials'}
response = requests.post('https://bitbucket.org/site/oauth2/access_token', data=data, auth=(key, secret))
print(response.json())
access_token = response.json()['access_token']
print(access_token)
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={"Bearer %s" %access_token})
print(groups.json())
This gives me an error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vagrant/projects/tools/axb-dsi-api/app/bitbucket_connector.py", line 35, in get_groups
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={"Bearer %s" %access_token})
File "/usr/local/lib/python3.6/dist-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/requests/sessions.py", line 494, in request
prep = self.prepare_request(req)
File "/usr/local/lib/python3.6/dist-packages/requests/sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 306, in prepare
self.prepare_headers(headers)
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 438, in prepare_headers
for header in headers.items():
AttributeError: 'set' object has no attribute 'items'
How do we use access token to call BitBucket API REST ?
EDIT
After correcting the header to json :
now it's saying
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vagrant/projects/tools/axb-dsi-api/app/bitbucket_connector.py", line 36, in get_groups
print(groups.json())
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
the headers parameter to the requests.get() should be an dictionary and you are sending set instead of the dictionary check your code it is headers={"Bearer %s" %access_token} which treated as set you can execute this on python and check.
You may need to use key:value check below example:
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={'access_token' : "Bearer %s" %access_token})
I successfuly get to retreive the data from Bitbucket API by passing the access token this way:
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/access_token={%s}"%access_token)
Use Authorization as a key in the header instead of access_token try below code.
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={'Authorization' : "Bearer %s" %access_token})
What I am trying to do is check the subdomain value passed to the route methods just like explained here
Whenever I access the panel blueprint I receive this error: peewee.OperationalError: Connection already open
This is how I initialize the database in my main app file.
flask_db = FlaskDB(app)
database = flask_db.database
The problematic code seems to be
#panel.url_value_preprocessor
def check_client_and_user(endpoint, values):
client = Client.get(Client.subdomain == values.pop('client'))
The full source code can be found on Gitlab
__init__.py is the main app file. panel/__init__.py has the problematic code. This is the full traceback I receive:
Traceback (most recent call last):
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1610, in full_dispatch_request
rv = self.preprocess_request()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1831, in preprocess_request
rv = func()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/playhouse/flask_utils.py", line 171, in connect_db
self.database.connect()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/peewee.py", line 3679, in connect
raise OperationalError('Connection already open')
peewee.OperationalError: Connection already open
I don't know where the extra connection is coming from. It seems like it is being generated by the Client.select() query.
Edit:
This code here
def get_or_404(client):
app.logger.debug(database.get_tables())
cli = Client.select().where(Client.subdomain == client)
Generates this response
--------------------------------------------------------------------------------
DEBUG in models [/home/marten/Projects/Comp/server/app/panel/models.py:14]:
['client', 'role', 'user', 'userroles']
--------------------------------------------------------------------------------
127.0.0.1 - - [17/Aug/2017 14:57:31] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1610, in full_dispatch_request
rv = self.preprocess_request()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1831, in preprocess_request
rv = func()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/playhouse/flask_utils.py", line 171, in connect_db
self.database.connect()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/peewee.py", line 3679, in connect
raise OperationalError('Connection already open')
peewee.OperationalError: Connection already open
As you can see the database is actually connected, and the client table is available.
Edit 2:
Well, I just found that apparently just database.get_tables() will cause this error. However, that code does fetch all tables from the database...
Does url_value_preprocessor run before the database is initialized? That would be strange since I import the object calling this decorator after initializing the database.
I'm attempting to use sendmail within flask to send some emails - but the error message coming back are a bit cryptic.
from flask.ext.sendmail import Message
from flask.ext.sendmail import Mail
mail = Mail()
mail.init_app(app)
msg = Message("Hello".encode('utf-8'), sender="xxx#xxx.com".encode('utf-8'), recipients="xxx#xxx.com".encode('utf-8'))
msg.body = "testing"
msg.html = "testing"
mail.send(msg)
The problem appears at mail.send(msg). Here is the full trace:
Traceback (most recent call last):
File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/xxx/xxx/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/xxx/xxx/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/xxx/xxx/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/xxx/xxx/login.py", line 129, in registersub
mail.send(msg)
File "/home/xxx/xxx/lib/python3.4/site-packages/flask_sendmail/mail.py", line 40, in send
message.send(self.connect())
File "/home/xxx/xxx/lib/python3.4/site-packages/flask_sendmail/message.py", line 115, in send
if self.is_bad_headers():
File "/home/xxx/xxx/lib/python3.4/site-packages/flask_sendmail/message.py", line 72, in is_bad_headers
if c in val:
TypeError: 'str' does not support the buffer interface
Where is the likely culprit?
Have a look at this example
>>> 'c' in b'val'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface
It's the same error you've got, so don't encode the str values you're using.
I have an email confirmation feature on my Flask application. For this to work, I must create a token which will go in a confirmation link. To create the token I'm using Its Dangerous like so:
from itsdangerous import URLSafeTimedSerializer
ts = URLSafeTimedSerializer(app.config["SECRET_KEY"])
token = ts.dumps(email, salt='email-confirm-key')
confirm = url_for('confirm', token=token, _external=True)
After running this, I receive an error stating cannot concatenate 'str' and 'NoneType' objects from the following traceback:
Traceback (most recent call last):
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/pavsidhu/Documents/Web-Development/myapp/myapp/views/confirmation.py", line 62, in resend
activateEmail(email)
File "/Users/pavsidhu/Documents/Web-Development/myapp/myapp/views/functions.py", line 34, in activateEmail
token = ts.dumps(email, salt='email-confirm-key')
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/itsdangerous.py", line 566, in dumps
rv = self.make_signer(salt).sign(payload)
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/itsdangerous.py", line 412, in sign
return value + sep + self.get_signature(value)
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/itsdangerous.py", line 347, in get_signature
key = self.derive_key()
File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/itsdangerous.py", line 334, in derive_key
self.secret_key).digest()
TypeError: cannot concatenate 'str' and 'NoneType' objects
I'm unsure what the issue is, as email is a string and the salt is one too. What could be the problem? Thanks.
The issue is this line:
ts = URLSafeTimedSerializer(app.config["SECRET_KEY"])
It looks like your app.config["SECRET_KEY"] is not being set correctly. If you replace that line with this
ts = URLSafeTimedSerializer('test')
You should find that it works. So you need to find out why app.config["SECRET_KEY"] is not being set correctly.