Passing data between apps in Python - python

I have a question about implementing passing data between two apps that I have running.
I have a Flask backend, which receives a user email via a POST request and stores it in a variable. I need to pass this variable into TelegramBotAPI.
What I need to happen is when Flask receives a new user email, it will pass the data into the TelegramBotAPI and trigger a function which will send it in a message to the user.
How do I go about implementing this?

Assuming your architecture separates the TelegramBot from the Flask server, you would treat your flask server as the client for your Telegram Bot which is the server in this scenario.
Armed with this information there are numerous ways to go about this. TelegramBot could provide an http server through which clients could send requests. Another option which requires less availability from the Bot but ensures eventual consistency is using a message broker like RabbitMQ.
Lastly, if you wish to store the email permanently you should consider using a storage like a Database (for robustness) or a File system (if you have only one server)

Related

Share a session between 2 flask apps running on different servers

I am using client side sessions. The requirement is to redirect from 1 flask server which already have a user session data to another flask app on a different server and use the same client session information to make sure the user has already logged in if not send them back to the 1st server for authentication.
If possible i would like to keep using the client side sessions. If not any information regarding the alternative will be helpful.
Thank you
Normally there is 2 options.
First, client side authentication using token like JWT (Json Web Token), this approach authenticate every request using token included in header and no need additional server.
Second, server side approach with additional session store like Redis for multiple backends.

How to emit different messages to different users based on certain criteria

I am building a chat application using flask socketio and I want to send to a specific singular client and I'm wondering how to go about this.
I get that emit has broadcast and include_self arguments to send to all and avoid sending oneself, but how exactly would I go about maybe emitting to a single sid?
I've built this application using standard TCP/UDP socket connection where upon client connecting, there socket info was stored in a dictionary mapped to their user object with attributes that determined what would be sent and when I wanted to emit something to the clients I would iterate through this and be able to control what was being sent.
I'm hoping some mastermind could help me figure out how to do this in flask socket io
To emit to a single user, you can use the user’s sid. For more info on that, visit this discussion.
If you have access to the client application, you could set up different rooms, let the clients join the different rooms according to your criteria, and have your server application emit to the different rooms. For more info on that you can have a look at the socket.io documentation about rooms and namespaces and the python-socketio documentation about rooms.
I ended up figuring it out. Using the flask request module, you can obtain the users sid using request.sid, which can be stored and emitted to within the room parameter emit(..... room=usersid

Django: Should sendgrid emails always be sent from Celery?

I'm using django-sendgrid-v5 and I read somewhere that it isn't good to send emails from the main webserver. Should I process emails from Celery? Or is it fine to call from the main app since I'm using an external service like Sendgrid anyways?
I don't know in which context you've read that, but I would guess it has something to do with reliability, spam and security in general.
Short answer: Yes, this should be fully okay as you are using an external email service.
Another option is to set up a Smart host on your webserver and let your main email server deliver it to the final recipients.
Long answer: Nowadays sending emails from a (web)server, which is not fully set up as an email server might be difficult in means of reliably sending emails.
Due to the massive amounts of spam and malware sent, most (or at least a lot) receiving email servers (Mail Exchangers) are trying to check if the emails they should deliver to their users, are legit.
This is done by several settings mostly on the server itself. To name only a few: RDNS, DKIM, Greylisting, etc.
In general a (web) server whos main purpose is not sending emails, does not have all these settings. This might result in difficulties to reach certain email addresses.

Restrict Python API access to single Arduino entity

I am basically writing an API in Python, using Flask, and I would like to restrict access to its endpoints so that only an entity, namely an Arduino, can have GET and POST access. How should I make this possible and what should I be looking for?
You can assign a secret key (i.e. a password) to each client of this API, and then require that this key is provided by the client with all requests sent to the server.
So you start by assigning a key to your Arduino client, but you can also give a different key to your own testing client, and any other development clients you may need.
The secret key can be passed as HTTP Basic Authentication with every request.
Configure your WSGI container or its associated web server to only allow access to the Flask application from the IP address assigned to the Arduino's network interface.

Best way to do email scheduling on a python web application?

I have a Pyramid web application that needs to send emails such as confirmation emails after registration, newsletters and so forth. I know how to send emails using smtplib in python and I decided on an smtp service (I think sendgrid will do the trick).
The real problem is the scheduling and delay sending of the emails - for example, when a user registers, the email is to be sent on the form post view. But, I don't want to block the request, and therefore would like to "schedule" the email in a non-blocking way.
Other than implementing this myself (probably with a DB and a worker), is there an existing solution to email queue and scheduling?
Thanks!
The existing solution to which you refer is to run your own SMTP server on the machine, bound only to localhost to prevent any other machines from connecting to it. Since you're the only one using it, submitting a message to it should be close to instantaneous, and the server will handle queuing, retries, etc. If you are running on a UNIX/Linux box, there's probably already such a server installed.
In my app, I insert emails in DB table, and I have python script running under cron that checks this table and sends email updating record as sent.
You can also use Redis Lists as a Queue to send emails. Create couple of worker processes which listens a Redis List, and
publish a Email job via RPUSH or LPUSH
receive the job at your worker via LPOP or RPOP
so that your web app worker process won't be affected, or not even feel the overhead for email sending operations.
This design allows you not to care how long does it take to send an email. The email service might be local or a external email service, however you want.

Categories

Resources