INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Help calling a python script through AJAX

Help calling a python script through AJAX

(OP)
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:

CODE --> python

#!C:\Python27

import kipro

client = kipro.Client('http://10.221.14.161')
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:

CODE --> HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<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 
				<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"> 
				<div id="eParamID_CurrentMediaAvailable" class="element_text polling_frequency_9" style="display: inline;">
				</div>%
			</div>
		</div>
		<div id="reel">Reel 
			<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

RE: Help calling a python script through AJAX

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 http://localhost/cgi-bin/kipro/play.py .

Feherke.
feherke.ga

RE: Help calling a python script through AJAX

(OP)
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

RE: Help calling a python script through AJAX

(OP)
I got it working. I had to import the cgi library to my python script.

CODE --> python

#!C:\Python27\python.exe -u
import cgi
import kipro

client = kipro.Client('http://10.221.14.161')
client.play() 

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

Thanks

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close