Rithesh Bhat
Rithesh Bhat

Reputation: 127

Launch Python Scripts from Tomcat 8.5

I've built a simple webpage, which runs a simple python script on click of a button, Unforutnately, Tomcat doesn't doesn't execute the python script.

I've added the servlet mapping tags from conf/web.xml to WEB-INF/web.xml ( CGI Servlet tag) It displays content of my file, than executing the python script.

Here's the directory and files:

bash-3.2$ ls -ltr
total 3598
-rw-r----- 1 orte123i dbte123i 3579238 Mar 26  2018 jump.jpg
-rw-r----- 1 orte123i dbte123i    1615 Aug  6 10:49 theme.css
drwxr-x--- 2 orte123i dbte123i       4 Oct 18 10:06 META-INF
drwxr-x--- 2 orte123i dbte123i       3 Oct 18 10:06 cgi-bin
drwxr-x--- 2 orte123i dbte123i       7 Oct 19 06:35 js
-rw-r----- 1 orte123i dbte123i    1442 Oct 19 06:35 index.html

And the JS script used to call the script is

bash-3.2$ cat custom.js
$(document).ready(function(){
$(sub).click(function(ev){
alert("Button Clicked!!")
    ev.preventDefault(); // Stop the form from redirecting the page.
    $.ajax({
            type:'POST',
            url:'cgi-bin/test.py', 
            success:function (res) {
                 $('#output').html(res);
            }
    });

});
});

Python Script has just a print statement:

bash-3.2$ cat test.py
#!/usr/local/bin/python3.6
from threading import Thread, Event
import time
import threading, paramiko
import sys, os
import cgi, cgitb


print("Content-Type: text/html\n\n")
print("hello")
print("hello")

Here's the result on my webpage div tag.

#!/usr/local/bin/python3.6from threading import Thread, Event import time import threading, paramiko import sys, os import cgi, cgitb print("Content-Type: text/html\n\n") print("hello") print("hello") 

The code isn't executed, only the content of the file is displayed.

Upvotes: 2

Views: 796

Answers (1)

maQ
maQ

Reputation: 506

Why Python on Tomcat? Tomcat is for serving Java-based web pages. For serving Python you should use Python Flask library (can be also Tornado, Django, Pyramid). Even your JavaScript static file can be easily hosted using above.

Upvotes: 1

Related Questions