Use python to implement a async notification system - python

I have been troubled by a design question.
I want to implement a small service that looks like a mail office. It will accept clients request(in the request it has the message to deliver and destination url info). After that I will help the client to send the message to specified URL. BUT when the first trial fails(I expect a 200 with 'success' response) there is some strategy like I will continually send the message to the destination url at most 10 times. And I will wait for a certain time to send the message again. For example, I send the message immediately after I receive the request(the following looks like a delivery algorithm):
if it fails then
I will wait for 1 minutes to send it again
if it fails again then
I will wait for a longer time period(like 5 minutes) and so on
until the num of trials reaches Max(10 times).
I used to use django framework with django-cron to solve the problem. However it is not accurate for it depends on the time I set for the django-cron(the less time between the cron the better). I don't know whether there is an elegant and appropriate method to solve this kind of problem.
Thank you for your help!

Related

Waiting in a Django View For a Webhook or Timeout

I am using WhatsApp official API to send messages to customers. I want to send them a reminder message if they have not replied during a certain period of time (a couple of hours more or less). In other words, I will send a message and wait for a certain period of time for their response. If they respond, no reminder message is needed. If they don't, I will send them a reminder message. WhatsApp API sends webhook notifications when they reply.
How do I implement that in a Django view? I am thinking of creating an async process/thread each time I want to wait, and handle the timeout condition in that process somehow. Is this the right approach? I think that would be costly in terms of server time used, but I am not sure. If this is the right approach, please show some pseudo-code on how it can be done.
I will be deploying my app on Heroku.
Your answer is much appreciated..

Sending emails with outlook with files every month to same clients

I send reports every month to clients with outlook. Every client has a report. So every client receives a different report depending on his performance of the month.
For now I send an email manually to every client attaching manually their report. That takes me around 1-2 days every month. I have about 100 clients meaning I send 100 emails every month manually.
I would like to automate that task every month with Python. I would like to use Outlook as I am using it today. I am learning Python for now.
Is there a way to automate that? The input info is:
An email with the client's performance written in the email.
An attached file with the whole report (sometimes multiple reports).
Also have a double check that the report corresponds to the right client.
Thank you!
I believe you can get the anwser you are looking for in https://www.google.com/amp/s/www.freecodecamp.org/news/send-emails-using-code-4fcea9df63f/amp/
There you can get informations about how the mechanics of using python for emails works and how to use it. Cheers!

How can I bypass the 429-error from www.instagram.com?

i'm solliciting you today because i've a problem with selenium.
my goal is to make a full automated bot that create an account with parsed details (mail, pass, birth date...) So far, i've managed to almost create the bot (i just need to access to gmail and get the confirmation code).
My problem is here, because i've tried a lot of things, i have a Failed to load resource: the server responded with a status of 429 ()
So, i guess, instagram is blocking me.
how could i bypass this ?
The answer is in the description of the HTTP error code. You are being blocked because you made too many requests in a short time.
Reduce the rate at which your bot makes requests and see if that helps. As far as I know there's no way to "bypass" this check by the server.
Check if the response header has a Retry-After value to tell you when you can try again.
Status code of 429 means that you've bombarded Instagram's server too many times ,and that is why Instagram has blocked your ip.
This is done mainly to prevent from DDOS attacks.
Best thing would be to try after some time ( there might be a Retry-After header in the response).
Also, increase the time interval between each request and set the specific count of number of requests made within a specified time (let's say 1 hr).
Retry-After header is the best practice. However, there's no such response header in this scenario.

PYTHON : How to make my request.get() last for a few second?

To test my API, I need to send a request on my viewer url on which there is a tracking service that tell my API how many time I've spent on the page (classical).
I have this small function in my tests :
def does_it_track(response, **kwargs):
# some unrelated actions
r = requests.get('my_viewer_url')
This request works fine but it only last for less than a second and it doesn't allow me to test my statistic generator, neither the my tracker precision.
I've tried :
This SO issue : how to make python request.get wait a few seconds? it didn't help
The sleep method (but I got has no attribute 'sleep'
To repeat the request send, but it obviously create several stats and I only need a longer one
Does someone know about a "not-to-complicated-way" to make my request wait on my page ?
I'm python 2.7
Thank you !
"how many time you've spent on the page" has nothing to do with the HTTP request/response cycle, but with your browser.
From the server's point of view, the server gets a request, returns a response and the job is over, period - and from the client's point of view once the server returned a response the HTTP transaction is over too. There's not even a notion of "page" here, only HTTP request and response.
Your "tracker" is (obviously) using javascript to send data from the browser itself (most likely by sending a request each X seconds indicating the page is still displayed in the browser). IOW, the only way to test this is to use a headless browser that will execute javascript.
Try VCR, it might help you to solve your issue.
Indeed you would be able to save your request and see what's happening.
VCR

In a RESTful web service, is it acceptable for the server to take many minutes to respond?

I am developing a RESTful web service using flask-restful.
The client needs to be able to request a job to be performed by the server. This job can take anywhere from ~1 second to ~1 hour to perform. Generally, it is expected to take 1-5 minutes.
When the job is complete, the client needs to download a JSON dump. Anywhere from 100KB to 100MB in size.
I see 2 options:
The client submits the job as a POST request, and the response from the server only comes when the job is complete. The response includes the JSON dump.
The client submits the job as a POST request, and the server responds immediately with a 200 OK. The client then submits a GET status request every, say, 60 seconds. When the job is complete, it submits another GET request to download the JSON dump.
Which option is preferred under REST principles?
The problem I see with Option 1 is the possibility of network disruption whilst waiting for the response.
Waiting more than a few seconds is an absolute no-no.
Most of the web infrastructure is not designed to handle such long delays, and some proxies/load balancers may timeout - even if your server finally produces the response, no-one will be there to read it. Moreover, the user will get bored and start refreshing/cancelling/whatever.
As #jonrsharpe mentioned in the comment, your server should respond as quickly as possible with information on what's happening. Enter 202 Accepted status code:
The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. There is no facility for re-sending a status code from an asynchronous operation such as this.
(taken from restapitutorial)
So respond with 202 and a handle to where the results should be - either in body or in response headers. Then the client can poll given location to see the job status and download the results.
If the result is big, it's also reasonable to allow HEAD requests on the result, so that the user can poll HEAD to check if the results are available and then download them with GET, without being suddenly flooded during the polling.

Categories