How to Run a Simple Python Script non-stop 24/7 - python

I have a simple python bot that I want to run non-stop 24/7. I haven't been able to figure out which GCP product would be best for this and how exactly I could set it up to work in this way. Any help or a detailed explanation would be greatly appreciated.

Deploy a VM on Google Cloud, ssh into your VM and put all the relevant code in there, then run your python bot as you normally would

Although not a Google product, you should also consider repl.it:
It allows you to host for free (unlike GCP), 24/7 with uptimerobot constantly pinging it.
But, it is also a security problem. Others can easily see your code
Therefore, I think it is really about "security" and "price" (how confidential is the source code of your bot).

Related

Send Data between EV3 and PC

I am programming a roboter, and it needs to send data between the EV3 and my laptop (Windows)
I run pybricks on the EV3, which enables me to code in python.
I already did research, but the only things that are remaining are some blogs from 2014 that don't help either.. the only thing that helped a little was the official documentation of pybrick.
I thought that running the example code on the EV3 and laptop would work, but the code only worked on the EV3. The EV3 waits until getting a message, but the laptop instantly says connected, even tough it isn't.
I thought it is maybe possible to get the laptop to act like an EV3 to connect them (because the original message function for EV3 is only made for interaction between different bricks), but my knowledge kinda ends here, even tough I tried a few things, like a virtual box.. maybe I did something wrong, but I hadn't had good results
If I understand correctly, you want to,
send a value to a remote machine
have the remote machine do some stuff
get a response from the remote machine
If that is correct, this is normally achieved by API. The remote machine serves an API, which the client calls using the requests library. For example, TheCatAPI allows you to call for pictures of cats. Your laptop and EV3 are still logically distinct, even though they are physically nearby.
You can choose to host the API on your laptop or make it available to others using services like AWS. Avoiding copypasta, Real Python has some fantastic docs on how to build an API.
You will want to connect your EV3 to WiFi then tell it to call your new API. If you set this up in AWS lambda, others will be able to do the same and it will probably be free for you.
If you want to do it purely locally, you will need to connect your EV3 and laptop via Bluetooth and establish a serial link.
Personal opinion
I would suggest that showcasing your ability to create an API and get it working on AWS would be more beneficial than going down the Bluetooth route.

How to run a program on a schedule - Google colab

Over the past few weeks I've been coding a program which runs a reddit bot locally from my machine. I've perfected it such that it does not reply to the same comment which it has replied to before, it runs quite efficiently, and in my opinion is complete.
Now, I'm looking for a way to get the program to run on a schedule. I currently have the code in google colab, and I don't know how to use google colab for this functionality.
The program does not require any local storage, it's one code file, and does not require much memory, so I wanted to ask if anyone has a resource which has an detailed tutorial accessible for beginners which I could use to host this code
Note: The code requires an installation of PRAW, in google colab I simply do !pip install PRAW if that means anything differently for what I need to do, what should I do differently?
Thank you in advance.
Google Collab is not designed for this kind of things, and most likely it cannot be used to run your app on schedule.
Probably the easiest solution is some kind of Continuous Integration tool that lets you run code remotely.
Step 1 would be to host your code to some remote code repository like GitHub. Since it most likely won't have to be interactive switching from Collab notebook to simple Python script will make your config much easier later on.
Step 2 would be connecting that repo to some CI tool. One I am familiar with that lets you run pipelines on schedule is CircleCI, with this tutorial here showing very simplistic configuration for running Python scripts from a pipeline.

Problem uploading a python script in Microsoft Azure using App Service

I make this topic as we have a problem uploading a python archive in Azure. We did a modular code with python, which is a text generator. The code is working on python environment, besides we wanted to upload it to the cloud. When we tried it, we used the App service of Microsoft azure, we didn’t have any trouble while we were doing the process this way. Nevertheless, at the end, the program gave us a json, when we tried to put that URL into the navigator… There’s were the problem is, the generated Jason doesn’t connect with the navigator. We don’t have any clue of what’s happening, because the console doesn´t give any warning or error.
If any of you knows or have a tip for us to solve this problem, I would appreciate it,
Thanks for your attention and have a nice day.
Welcome to Stack Overflow! From what I understand, I suppose you should be going with Azure Automation Runbooks instead of Azure App Service, if you intend to execute or automate a task-like job. You can also test your runbook and implement error handling streams to ensure it works as expected. Please refer to this example for a detailed walkthrough.
Please elaborate your requirement if this is not what you were looking for, and we can explore other alternatives.

Reddit Python Bot, why is it unsafe to store login credentials in the script?

I'm following along this tutorial: http://pythonforengineers.com/build-a-reddit-bot-part-1/ and in the Create Reddit App subsection it states that I should not have any login credentials directly in the script, because 'others might see them'.
Now, you need to update your praw ini file to remember these settings. Otherwise, you’ll have to put them in your script and thats dangerous (as others might see them).
Does my script get uploaded to Reddit, where it can then be read by other people? If not, why is it then unsafe?
If you're only developing this bot as a proof of concept and always running it from the computer that you're developing it on there is little risk that your credentials will be leaked. However most code doesn't live it's life as only one copy on only one system. The vast majority of modern software development best practices are based around the assumption that different computers will develop the code, store versions of the code and actually run the code.
If you hard code your login credentials in your login script and are only running it from your computer this isn't going to be an issue. What if you need help debugging your bot? What if you're really proud of it and want to show it off? What if you want to store a backup on another system? What if you don't want to have it running on the same computer you developed it on? Suddenly code containing your login credentials are getting shared across multiple computers. The more systems your code is on the easier it will be for someone to get access to them. This is why it's a software development best practice to not hard code secrets, like login credentials, in scripts, or code.
In the case of the tutorial you're following they're instructing you to run your code on a VM and use git for source control. As soon as you commit your credentials into git they will always be a part of that repositories git history. If you ever host your repos on a remote system such as GitHub your credentials will be stored on a system outside your control.

Is there a way to keep Telegram bot running when closing Python? [duplicate]

This question already has answers here:
How to do parallel programming in Python?
(10 answers)
Closed 5 years ago.
I've built a very simple Telegram bot by following this tutorial. So I have a file containing Python code, and when I run that code, the bot will echo what I say.
Is it true that the bot will only work when I have Python on and the code running? Would this mean that I cannot run any other script in Python at the same time, and neither can close Python down if I want my bot to keep working?
Is there any way to get around this, so that the bot will always be 'on'?
A Telegram bot is a Python program. When you run it, it do what it is supposed to do, then, if you stop the program, the bot stop to work. The problematic is common to all programs, particularily on a server. Think about Nginx, Apache, ssh, etc. Thay are all programs, and they all stop to do their job when they are closed.
If you want to make sure your bot will run always, you have to daemonize it. There is a lot of solutions.
You could transform your script to be a daemon, so when you launch it, it go directly to the background and continue to run until the server is shut down (or the program crash). But in that case, do your bot will re-run if you (or somebody else) restart the computer (server) ? There is some python libraries for this purpose, like daemonize.
Another common solution is to run your bot in a process manager. You can check supervisorctl for example, or you could decide to create a script to run your program from System V, UpStart or Systemd... This suppose you want to deploy your bot on a dedicated server or a VPS. This will be covered by the part 3 of the tutoriel you followed:
The next and final part of this series will [...] be demonstrating how to deploy the Bot to a VPS.
You could also consider encapsulating your bot into an image or a container (Docker, etc.) to run it on a compatible platform.
You should not have a problem running two consoles in Python, on your computer, at least. Your code should only run when Python is open on your computer, correct. As Eli correctly pointed out, a daemon would be suitable if you wanted to host locally.
However, what gets difficult is if you want to have it continuously running online. For example, with Reddit bots that search and post comments on posts, you need to host these through some cloud based service. I suggest using Amazon Web Services, which has a free trial giving you more than enough for basic Python needs. Some people will also use Heroku. Pretty much you're able to save the state of your current Python window, and it will run constantly.
I'd check out this post to see how to set up "screen" in AWS.

Categories