Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help calling a python script through AJAX

Status
Not open for further replies.

deeciple

Technical User
Mar 1, 2012
70
US
Hi All,

I have been wrestling with trying to get a simple python script to run on the click event of an HTML button. Here is the python script:

Python:
#!C:\Python27

import kipro

client = kipro.Client('[URL unfurl="true"]http://10.221.14.161')[/URL]
client.play()

It issues a "play" command to a remote VTR. I have the script in my server's "cgi-bin" folder and I have the cgi module enabled in my httpd.conf file. I am trying to call the script through AJAX that is executed through the onclick event of an HTML "play" button.. Here is my page:

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>AJA Ki Pro</title>

<link href="css/index-iui.css" rel="stylesheet" type="text/css" media="screen" />
<!--	<script type="text/javascript" src="js/kipro.common.js"></script> -->
<!--	<link rel="shortcut icon" href="/favicon.ico?" type="image/x-icon"/> -->
<script type="text/javascript">

function play(){
  var cmdPlay = new XMLHttpRequest();
   cmdPlay.onreadystatechange = function() {
    if (cmdPlay.readyState == 4 && cmdPlay.status == 200) {
     var result = cmdPlay.responseText;
    }
  };
  cmdPlay.open("GET", "C:/wamp/cgi-bin/kipro/play.py", true);
  cmdPlay.send();
}

</script>
</head>

<body style="overflow: hidden; background-color: #000000;" orient="landscape">
	    
    <form id="home" class="dialog" selected="true" action="">
<!--	<a class="button blueButton" type="cancel">Close</a> -->
    	
    <div id="player" class="transportPlayer">
		<div id="transport_display" class="transportDisplay">
			<div id="clip">Clip&nbsp;
				<div id="eParamID_CurrentClip" data-paramid="eParamID_CurrentClip" class="eParamID_CurrentClip element_text polling_frequency_1" style="display: inline;">
				</div>
			</div>
			<div id="media_stats">
				<div id="eParamID_SelectedSlot" class="element_text polling_frequency_5" style="display: inline;">
				</div>
				<div id="media_info_controls_page" class="available_media_info">&nbsp;
				<div id="eParamID_CurrentMediaAvailable" class="element_text polling_frequency_9" style="display: inline;">
				</div>%
			</div>
		</div>
		<div id="reel">Reel&nbsp;
			<div id="eParamID_VolumeName" class="element_text polling_frequency_5" style="display: inline;">
			</div>
		</div>
		<div id="timecode">
			<div id="eParamID_DisplayTimecode" class="element_text">
			</div>
		</div>
	</div>
			
<!--	<div id="transport_buttons" class="transportButtons"> -->
		<div id="reverse"      class="transportButton reverseButton"></div>
		<div id="play"         class="transportButton playButton" onclick="play()"></div>
		<div id="forward"      class="transportButton forwardButton"></div>
		<div id="stop"         class="transportButton stopButton"></div>
		<div id="record"       class="transportButton recordButton"></div>
		<div id="select_up"    class="transportButton selectUpButton"></div>
		<div id="select_down"  class="transportButton selectDownButton"></div>
		<div id="delete_clip"  class="transportButton deleteClipButton"></div>
<!--	</div> -->
			
	<div id="kipro_transport" class="kiproLogo"></div>
    </form>
</body>
</html>


If I double click the python script directly it does work and a "play" command is issued to the target machine... and it plays :)

Any help is appreciated as I am just learning python.

Thanks,

Ken
 
Hi

That "C:/wamp/cgi-bin/kipro/play.py" does not look good. Even if your browser is kind to translate it internally to URL file://C|/wamp/cgi-bin/kipro/play.py, the best you can achieve that way is to get the source code of the Python script.

The browsers are just clients, they only execute code that they can interpret themselves or through installed plugins. That Python script is actually a CGI script and has to be executed by the server. But file: protocol is served directly from the local file system, so the server never gets involved. You have to request that file through HTTP, so will need an URL like [ignore][/ignore] .

Feherke.
feherke.ga
 
Hi feherke,

Thanks for pointing that out. I had not thought about that link being on the client and thus being invalid. I changed the link but it's still not working for some reason. I am using WAMP server for my development. It supports cgi.

I will do some more research on getting CGI running on it, as I cannot even get a test "hello world.py" script running right on it so I suspect there lies the problem.

Thank you for your help :)

Ken
 
I got it working. I had to import the cgi library to my python script.
Python:
#!C:\Python27\python.exe -u
import cgi
import kipro

client = kipro.Client('[URL unfurl="true"]http://10.221.14.161')[/URL]
client.play()

This leads me to another question about retrieving URL parameters but I will post again for this.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top