I'm trying to email an output of a Python script that I made, however I want to do this periodically without needing to start the script manually.
At the moment I have an Apple script which runs the appropriate Python script and sends this to multiple people. The Python script uses an API to get some information and prints a message to the stdout, which gets sent to the appropriate people with the following simple bash script:
python3 myprogram.py | mail -s "Subject" myself#mycompany.com
This does the trick, but I want it automated even when my computer is not running.
Is there a standard way of making this possible that I don't know of? Is there a better way of periodically sending emails that I could use together with the script?
If there is a better language for this than Python, please do recommend it, I like to learn :)
Yes, if you need it to be running even when your personal computer is not running, you should take a look at deploying the script to a server.
You can have your own server, or pay to use one. Heroku has a really nice free plan to get started.
Check this.
Related
I have a web server which I have developed an application on using php and SQL, mainly picked php as I am more comfortable with it.
In short the application automates some of our network tasks .
As part of this I have automated some solarwinds tasks and the library orionsdk doesnt have a php library so I have used python.
It's all working fine but I really need to run these python scripts from my browser .
I have considered using php shell exec and got my python scripts to accept args so I can run them and parse the output.
I know I could also use flask or django but worry I will have a flask app to maintain aswell as a php app.
I think the question is what the best way to achieve this or any way which I haven't mentioned .
Any help would be very much appreciated
So you want PHP to communicate with Python and you've already mentioned using shell commands and http traffic.
I can imagine you could also achieve something similar by connecting up both PHP and Python up to the same database. In that case PHP could write a record in a table and Python could pick that up and do something with the data in there. Python could be either be a long-running process or fired off by a cronjob in this case. If a database seems overkill you could also write a file to some place on disk, which Python can pick up.
Personally I'd go for the shell exec approach if you want to keep it light weight and for a API connection if you want to have a more robust solution which needs to be expanded later on.
I have a server side Python script that imports a big package called nltk. It ran at a command prompt, but would not run in Apache server.
I tried the logging and put it as the first import and created a log file immediately after. But nothing is written to the file before the script crashes.
Is there a way to see the "ImportError: no module ..." when the script runs on Apache?
As far as I'm aware, there's no built-in way to tell Apache to simply dump error log messages directly to the browser. I'm betting the error_log output happens in an independent part of the Apache pipeline -- just a guess.
Most of the time when someone wants output errors directly to the browser, that person is a developer working in a web script such as PHP, Python, Perl, or other lang. In order to output error messages that occur within your lang interpretter, you have to format the output and pass it through apache as if it was normal output.
PHP
Some languages provide an easy way to do this, such as the well known error_reporting switch in PHP.
Python
Unfortunately some languages, make this painful or almost impossible and Python is one of them.
At one time this was apparently possible using https://docs.python.org/3/library/cgitb.html
However that library has been deprecated. Seems like the Python community doesn't have much love for developers who want to get immediate feedback about errors in the browser. :(
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.
I have a Python Script that every time I run it, collects data from different sites, stores them into a file and than runs some analysis.
What I want to do next is to somehow install Python and all the packages that I need on a server for example and create a task, let`s say that everyday at 3 p.m, the code I have written executes without me being around and the data and results should be stored into a table.
My questions would be is this is doable?
Yes there is a way to do this. if you are on a server running some form of linux you can use crontab. As for server hostage, I don't know of any free servers but there is always servers for small fees.
I am attempting to build an educational coding site, similar to Codecademy, but I am frankly at a loss as to what steps should be taken. Could I be pointed in the right direction in including even a simple python interpreter in a webapp?
One option might be to use PyPy to create a sandboxed python. It would limit the external operations someone could do.
Once you have that set up, your website would take the code source, send it over ajax to your webserver, and the server would run the code in a subprocess of a sandboxed python instance. You would also be able to kill the process if it took longer than say 5 seconds. Then you return the output back as a response to the client.
See these links for help on a PyPy sandbox:
http://doc.pypy.org/en/latest/sandbox.html
http://readevalprint.com/blog/python-sandbox-with-pypy.html
To create a fully interactive REPL would be even more involved. You would need to keep an interpreter alive to each client on your server. Then accept ajax "lines" of input and run them through the interp by communicating with the running process, and return the output.
Overall, not trivial. You would need some strong dev skills to do this comfortably. You may find this task a bit daunting if you are just learning.
There's more to do here than you think.
The major problem is that you cannot let people run arbitrary Python code on your webserver. For example, what happens if they do
import os
os.system("rm -rf *.*")
So clearly you have to run this Python code securely. But then you have the problem of securing Python, which is basically impossible because of how dynamic it is. And so you'll probably have to run the Python shell in a virtual machine, which comes with its own headaches.
Have you seen e.g. http://code.google.com/p/google-app-engine-samples/downloads/detail?name=shell_20091112.tar.gz&can=2&q=?
One recent option for this is to use repl.
This option is awesome because the compilers are made using JavaScript so the compilation and execution is made in the user-side, meaning that the server is free of vulnerabilities.
They have compilers for: Python3, Python, Javascript, Java, Ruby, PHP...
I strongly recommend you to check their site at http://repl.it
Look into LXC Containers. They have a pretty cool api that you can use to create lightweight linux containers. You could run the subprocess commands inside that container that way the end user could not mess with your main server.