Jump to content

Run Python From Php


xittle

Recommended Posts

I've been setting up a notification system for users on a service I have set up on heliohost, but have encountered a major issue. I am sending mail via a python script (mail.py), which takes several arguments and sends the mail, but my php cannot use shell_execute or system to run this script. What is a method that is allowed on heliohost?

Link to comment
Share on other sites

Update: I tried krydos's solution but I'm getting a 500 internal server error.

Help? Here is the python file (in cgi-bin):

#!/usr/bin/env python
import cgi
import smtplib
argv=cgi.FieldStorage();
s=smtplib.SMTP("mail.smtp2go.com", 2525);
s.login((my username),(my password));
s.sendmail(argv["sen"],sys.argv["rec"],sys.argv["mess"]);
print("Content-type: text/html")
print();
print(argv);
Link to comment
Share on other sites

You've got a syntax error.

root@johnny [/home/xittle/public_html/cgi-bin]# /usr/bin/python mail.cgi
  File "mail.cgi", line 6
    s.login((my username),(my password));
                       ^
SyntaxError: invalid syntax

 

#!/usr/bin/env python

 

Also, I want to point out that if you use this shebang you're using a truly ancient version of python 2.6.6 which was released in 2010. I recommend you use /usr/bin/python2.7 or /usr/bin/python3.6
Link to comment
Share on other sites

I changed it to that after 2.7 didn't work, knowing on some machines the actual executable is named python27. It was a tweak that didn't work. I just changed it back, and it doesn't work as before.



Partially Solved: I had tried 2.7 and 27 but didn't touch 3.6, which worked in the end. However, now the smtp part won't work.

Edited by xittle
Link to comment
Share on other sites

We're working through it one issue at a time. I usually stop looking for problems as soon as I find one.

 

The next issue I see is you're using the wrong type of line endings. The easiest way to fix this is to create the file through cpanel file manager. Then click the code editor and copy/paste your code in. The default encoding and line endings that cpanel file manager uses is compatible with cgi. The other option is you can use a linux capable editor like notepad++ instead.

Link to comment
Share on other sites

Fixed line endings, however I still have the smtp problem.



I added some comments to my code to show where the problem is happening,

#!/usr/bin/python3.6
import cgi
import smtplib
argv=cgi.FieldStorage();
#all commented lines other than this one are commented because they would return an error 500
#this line will not work s=smtplib.SMTP("mail.smtp2go.com", 2525);
#this line depends on 5 s.login("BitDragon_Server",My_private_password");
#this line depends on 5 s.sendmail(argv["sen"].value,argv["rec"].value,argv["mess"].value);
print("Content-type: text/html")
print();
for element in argv.keys():
	print(argv[element].value;
Link to comment
Share on other sites

# /usr/bin/python3.6
Python 3.6.1 (default, Jul 1 2017, 22:46:39)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import smtplib
>>> s=smtplib.SMTP("mail.smtp2go.com", 2525);
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python3.6/lib/python3.6/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/python3.6/lib/python3.6/smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/python3.6/lib/python3.6/smtplib.py", line 306, in _get_socket
self.source_address)
File "/usr/local/python3.6/lib/python3.6/socket.py", line 722, in create_connection
raise err
File "/usr/local/python3.6/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
That would be because port 2525 is closed. Is there any particular reason it's on such a strange port? Does 25, 465, or 587 work for this service. Those are standard smtp ports.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...