Solved

Python CGI to Terminal Emulator

Posted on 2014-03-23
7
631 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

772 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