Solved

Python CGI to Terminal Emulator

Posted on 2014-03-23
7
615 Views
Last Modified: 2014-04-14
I have a Python CGI that needs to send a lot of form data to a terminal emulator.  I was attempting to use Windows Script Host to do this, which works fine from the command line, but does not work through the CGI.  Is there an alternative way to accomplish this task?  Is it also possible that this is a web browser permissions issue?
0
Comment
Question by:front71
  • 5
  • 2
7 Comments
 
LVL 9

Expert Comment

by:techtonik
ID: 39955212
It is hard to say what you need to do exactly and what fails (how it 'does not work') without some bits of code. You may try to use CGi server written in Python for debugging. It may be easier to see where the bug happens. Python 2 ships with CGI server that can be executed with:

python -m CGIHTTPServer

Open in new window

0
 

Author Comment

by:front71
ID: 39956298
Thank you for replying, techtonik.  There is only one instance of Notepad running.  I'm using Python 2.7.3 64-bit, Firefox, and IIS on a Windows 7 machine.  Below is my entire code.  I hope that it clarifies things for you.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys, os
import cgi
import cgitb

import pythoncom

import win32api
import win32com.client

def testFcn():
   ok = False

   try:
      shell = win32com.client.Dispatch('WScript.Shell')
      ok = shell.AppActivate('Notepad')
      win32api.Sleep(500)
      shell.SendKeys("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
      win32api.Sleep(500)
      shell.SendKeys("{ENTER}")
      win32api.Sleep(500)

   except pythoncom.com_error,(hr,msg,exc,arg):
      myStr += "Error"
     
   if ok:
      myStr = "Success"
   else:
      myStr = "Failed"

   return myStr

#-------------------------------------------------------------------------------
# MAIN
#-------------------------------------------------------------------------------
aStr = testFcn()

print """
<!DOCTYPE html>
<html>
   <head>
      <title> Test </title>
   </head>
   <body> {0} </body>
</html>
""".format(aStr)
0
 
LVL 9

Expert Comment

by:techtonik
ID: 39956751
Wow. I am pretty sure that for security reasons web servers should not allow to call win32com stuff in CGI scripts. What webserver do you use? Is there a stack trace or something? What happens if you don't catch the exception?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:front71
ID: 39962558
It's an IIS box.  The script definitely works via terminal / command line, just not in a web browser.
0
 

Accepted Solution

by:
front71 earned 0 total points
ID: 39989110
I was able to accomplish my task by going through ActiveX.  Thank you mods for trying to help.
0
 

Author Comment

by:front71
ID: 39989115
Please close this question since I was able to solve it on my own.
0
 

Author Closing Comment

by:front71
ID: 39998531
I was able to solve it with a different method and no one suggested anything that was helpful.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now