python as a shell like program (from server)??

19 views
Skip to first unread message

ejaz shah

unread,
Oct 7, 2009, 2:59:12 PM10/7/09
to mod...@googlegroups.com
Hi,
 
I want to use python as a "shell like" program(from server) and execute an external program in it( such as notepad, mmt-a testing tool etc). I have apache server (on windows xp) with mod_wsgi integrated with it.
I tried using this:
 
import os
def application(environ, response):
# os.execv('c:\mmt.bat')
# subprocess.Popen('C:\mmt.bat')

 text ="Hello Everyone"
 response('200 OK', [('Content-Type', 'text/plain;charset=utf-8'),('Content-Length', str(len(text)))])
 return [text]

But it doesnt seem to be working.
I was just thinking do I have to make change in the response paramter? How can I manipulate the response and return lines to get the desired functionality?
 
Kindly help me
 
Regards,
 
Ejaz Shah

ejaz shah

unread,
Oct 7, 2009, 3:13:47 PM10/7/09
to mod...@googlegroups.com


Or to be more specific and clear instead of mmt.bat make it any text file for eg. hello.text for which the path is C:\hello.txt.
Now in the above problem how can we execute this text file(i.e it opens up the hello.txt in notepad environemnt) from server?
 

gert

unread,
Oct 7, 2009, 6:26:10 PM10/7/09
to modwsgi
So you want to edit files on the server by clicking on it in a
webbrowser?
First I suggest using a ftp server, if you still want to open files
using a http server, then this is a example how to edit one file
vi.txt trough vi.htm

http://code.google.com/p/appwsgi/source/browse/#hg/appwsgi/www/vi

Yes it involves 2000 lines of code just to edit one file :)

ejaz shah

unread,
Oct 7, 2009, 8:10:06 PM10/7/09
to gert.c...@gmail.com, mod...@googlegroups.com
Not exactly editing. Like an icon on window, when we click on it, it opens up that application. I want a similar script on server, that if clicked opens up the application specified in the script.
 
Is that possible?
 
I tried something like this:
 
def application(environ, response):
 os.system('c:\windows\notepad.exe') 
 text ="When that Aprill with his shoures soote"

 response('200 OK', [('Content-Type', 'text/plain;charset=utf-8'),('Content-Length', str(len(text)))])
 return [text]

It's  not working. Any idea why? Do we need to import any library for this?
 
 
All other scripts like copying files from one directory to another on the client machine (by specifying the path in script) works on clicking the server script. But when it comes to running application like notepad etc it doesnt open up?
 
 
Regards,
 
Ejaz Shah
 

Graham Dumpleton

unread,
Oct 7, 2009, 9:00:04 PM10/7/09
to mod...@googlegroups.com, gert.c...@gmail.com
2009/10/8 ejaz shah <ejazs...@gmail.com>:
> Not exactly editing. Like an icon on window, when we click on it, it opens
> up that application. I want a similar script on server, that if clicked
> opens up the application specified in the script.
>
> Is that possible?

Am still confused about what you want to run where.

Is the web server on a different machine to where the user will be
accessing it from a web browser?

If they are different, right now your web script is going to run it on
the web server box and isn't going to be displaying on or running on
the users machine, even if you could get it to run.

If you do still want it to run on the server machine, who is even
going to be there to see it?

If they are the same machine, your only option may be some sort of
ActiveX control given it is Windows.

Graham

ejaz shah

unread,
Oct 7, 2009, 10:49:06 PM10/7/09
to mod...@googlegroups.com
Ok. Now I will try to explain this for the final time since I think I have bothered you guys enough.I am sorry for that.
 
The situation is as under:
I have a server setup on my window XP machine. It is used for uploading, downloading and deleting files(all these functionalities working fine). Now what I want is to have python scripts that perform certain task.  The place where all python scripts are located is:
 
http://localhost:8085/test/Upload/pythonlist.php (print screen of how this location displays scripts is attached as pic1).
 
The two scripts with the name hello.wsgi/copy of hello.py is the same file (only extension changed for test purpose.)
The complete script is as under:
 
def application(environ, response):
    text ="Hello Everyone"

    response('200 OK', [('Content-Type', 'text/plain;charset=utf-8'),('Content-Length', str(len(text)))])
    return [text]
 
Clicking on this script(hello.wsgi or copy of hello.py)  gives the output which prints  Hello Everyone!!!
 
 Now what I want is to have a script at this location(http://localhost:8085/test/Upload/pythonlist.php), clicking on which shall open up notepad for the user.
 
I tried using this script:
 
import subprocess
import os
import traceback
 
def application(environ, response):
      try:
        file.write("im here\n")
         subprocess.Popen('c:\WINDOWS\notepad.exe')               
    except:
        if traceback.print_exc(10) != None:
            file.write(traceback.print_exc(10))
        else:
            file.write("nothing happened")
     file.close()

     text ="When that Aprill with his shoures soote"
     response('200 OK', [('Content-Type', 'text/plain;charset=utf-8'),('Content-Length', str(len(text)))])
     return [text]
   
 
 The desired output is that, on clicking this script it shall print out text (not in notepad) and open up notepad.
But what it does is, it prints out the text and I dont know why it runs the notepad application in the background instead.
You cant see any notepad opening up but when you check it in task manager it shows that notepad is running.
 
So I would say the only problem now left is how to get the application (notepad) to run in front (as in openup notepad on click) rather than running in the back ground.
I hope it will clear out the situation as to what I am trying to do. I am sorry for not explaining it well before.
pic1.bmp

Graham Dumpleton

unread,
Oct 7, 2009, 10:59:58 PM10/7/09
to mod...@googlegroups.com
2009/10/8 ejaz shah <ejazs...@gmail.com>:
So, every time some arbitrary user on a remote system runs that script
via your web page, you want a notepad instance to run. Sounds like a
really bizarre and dangerous thing to do. Get enough people doing it
at once and could kill the box due to number of spawned processes.

> But what it does is, it prints out the text and I dont know why it runs the
> notepad application in the background instead.
> You cant see any notepad opening up but when you check it in task manager it
> shows that notepad is running.

As I said once before, the problem you are likely going to have is
that Apache runs as a distinct user from that who may be logged in at
the console. That user will not have the environment of the logged in
user. One consequence of that is that it may not be able to run up a
GUI based program, because if Windows is anything like MacOS X or X
Windows, the environment Apache is running in will not have the
appropriate environment settings to know how to connect to the display
of the logged in user, or will not have the privileges to bring up
Windows on the logged in users display.

In all, you probably want to go ask on a Windows group, 'how do you
startup a GUI based application from a daemon process such that it
will display on screen of logged in user'. This is very much a Windows
issue and nothing to do with Python, Apache, web applications or
mod_wsgi.

Graham

gert

unread,
Oct 8, 2009, 1:41:09 PM10/8/09
to modwsgi
Just make sure your header is text/plain and tell firefox / ie8 to
open all txt files in notepad instead.
You can test your browser first by clicking on this link. Fidel with
your browser until it opens in notepad.

https://appwsgi.googlecode.com/hg/license.txt

Subporcess notepad = bad idea

On Oct 8, 4:49 am, ejaz shah <ejazsha...@gmail.com> wrote:
> Ok. Now I will try to explain this for the final time since I think I have
> bothered you guys enough.I am sorry for that.
>
> The situation is as under:
> I have a server setup on my window XP machine. It is used for uploading,
> downloading and deleting files(all these functionalities working fine). Now
> what I want is to have python scripts that perform certain task.  The place
> where all python scripts are located is:
>
> http://localhost:8085/test/Upload/pythonlist.php(print screen
> On Wed, Oct 7, 2009 at 6:00 PM, Graham Dumpleton <graham.dumple...@gmail.com
>
> > wrote:
>
> > 2009/10/8 ejaz shah <ejazsha...@gmail.com>:

Graham Dumpleton

unread,
Oct 8, 2009, 6:08:29 PM10/8/09
to mod...@googlegroups.com
2009/10/9 gert <gert.c...@gmail.com>:
>
> Just make sure your header is text/plain and tell firefox / ie8 to
> open all txt files in notepad instead.
> You can test your browser first by clicking on this link. Fidel with
> your browser until it opens in notepad.
>
> https://appwsgi.googlecode.com/hg/license.txt
>
> Subporcess notepad = bad idea

That is assuming that he wants it on the client and not the server.

He never answered whether they were the same machine so it still isn't
clear what he wants or even really why he wants it.

Your method also assumes that he wants to open in NotePad the response
content and not use the request to just open NotePad by itself or some
other file on the server.

Graham

ejaz shah

unread,
Oct 8, 2009, 10:33:03 PM10/8/09
to mod...@googlegroups.com
The script will be run through remote access. It is not dangerous since server access will be via VPN network so few people has access to it. It is being used up for certain functionality so it doesntt really matter. The only functionality needed is that a script on server shall be able to run applications on client machine.
 
The code I sent before is working fine for all applications including notepad and some other softwares that I tested it for but the only problem is that it is not opening up the GUI of that particular application. For example there is a particular software that runs and saves results in excel sheet (on client machine). I ran that through that script. It ran properly and saved the result in the excel sheet but while running, it has a particular GUI(showing progress bar of calculation etc) that opens up. But when that script is executed on server side that GUI does not open up-it only runs in the background performs all calculation and saves result it in the excel sheet (opening and saving results-all this action is performed on the client machine that executes that script from server).
 
So any suggestion for that? Is that possible what I am trying to do?
 
It is not specific to any single application. With slight chagnes it shall be able to launch any client side application and shall show its GUI too. Thats all what I want to do. Right now its launching the application but is not showing the GUI.
 
Really appreciate your responses. I have done almost everything which couldnt have been possible without your help.
This is the only thing left for now.
 
Regards,
 
Ejaz Shah  

Graham Dumpleton

unread,
Oct 8, 2009, 11:07:40 PM10/8/09
to mod...@googlegroups.com
2009/10/9 ejaz shah <ejazs...@gmail.com>:
> The script will be run through remote access. It is not dangerous since
> server access will be via VPN network so few people has access to it. It is
> being used up for certain functionality so it doesntt really matter. The
> only functionality needed is that a script on server shall be able to run
> applications on client machine.
>
> The code I sent before is working fine for all applications including
> notepad and some other softwares that I tested it for but the only problem
> is that it is not opening up the GUI of that particular application. For
> example there is a particular software that runs and saves results in excel
> sheet (on client machine). I ran that through that script. It ran properly
> and saved the result in the excel sheet but while running, it has a
> particular GUI(showing progress bar of calculation etc) that opens up. But
> when that script is executed on server side that GUI does not open up-it
> only runs in the background performs all calculation and saves result it in
> the excel sheet (opening and saving results-all this action is performed on
> the client machine that executes that script from server).
>
> So any suggestion for that? Is that possible what I am trying to do?
>
> It is not specific to any single application. With slight chagnes it shall
> be able to launch any client side application and shall show its GUI too.
> Thats all what I want to do. Right now its launching the application but is
> not showing the GUI.
>
> Really appreciate your responses. I have done almost everything which
> couldnt have been possible without your help.
> This is the only thing left for now.

There is no way that you could be using a Python script like that on
server side to launch a program on client machine. If it is working, I
have absolutely no idea how you are achieving it. A server side script
can only run a server side application.

The only way I could see this working at all is that the user isn't
actually running stuff on their client machine but is using a citrix
client, Microsoft remote desktop or some other remote Windows desktop
mechanism hosted by the server and the client machine is merely being
used to display the full desktop session. Even then, you are going to
have to solve the issue of how the server side script is able to
direct the application to be opened in the correct users desktop
session.

Anyway, as long as you know what you are doing, cause I still have no
idea. As before, seems like this is a question for a Windows specific
group.

Graham

ejaz shah

unread,
Oct 12, 2009, 4:13:15 PM10/12/09
to mod...@googlegroups.com
Hi,
 
Thanks Graham. It was a window specific problem. You need to enable the server to interact with the desktop applications. So it is done now.
 
Now I need to perform this action through a remote access(not on the server machine now). For that I need a javascript embeded in python(or if there is any other way do let me know). I was using the following script. But it does not seem to be working.
 
I am not sure the way I am using javascript here is allowed or not. Need some guidance on this. I worked with php and you can use javascript in between php tags but I don't know how it works in python. My attempt of code is as under.
 
The code is as under:
 
def application(environ, response):
    print "<html>
            <head>
    <SCRIPT type="text/javascript" LANGUAGE="JavaScript">
    function executeCommands()
    {
// Instantiate the Shell object and invoke its execute method.
        var oShell = new ActiveXObject("Shell.Application");
        var commandtoRun = "C:\\WINDOWS\\Notepad.exe";
// Invoke the execute method.
        oShell.ShellExecute(commandtoRun,"", "", "open", "1");
    }
    </SCRIPT>
             </head>
             <body onload="executeCommands()">
             </body>
            </html>";
    text ="Hey It's Working!!!"

    response('200 OK', [('Content-Type', 'text/plain;charset=utf-8'),('Content-Length', str(len(text)))])
    return [text]
Need help.
 
Regards,
 
Ejaz Shah

Jason Garber

unread,
Oct 12, 2009, 5:19:29 PM10/12/09
to mod...@googlegroups.com
Hi Ejaz,

I think that you are at the point that this question is not mod_wsgi related.  The general job of WSGI is to manage the communication between the web server and web application.  You are talking about client side applications, javascript, and general python questions.

Keep in mind, that the only thing a web server is going to do is generate and output content - usually in the form of HTML.   It is the job of the **web browser** to do "something" with this content.  You see, they are totally separated.

-- Browser asks for content.  Server gives content.  Browser uses content. --

The languages or code that you use to GENERATE content have nothing to do with the consumer of the content (eg. the Web Browser).  The BROWSER has no knowledge of the how the content was made -- it just has the content.  Lastly, you cannot* run javascript on any web server.

* in any sanely useful way

----------------------------------------------
Here is what I strongly suggest.

1. Write down your requirements so that a complete stranger can understand them.
2. Use a real-life illustration, such as "User clicks http://test.com/abc in his browser.  Server responds by opening Microsoft Excel to ..."
3. Boil it down to a specific question for advice of how to accomplish what you want.

If you are clear and respectful, there is a very high chance you will get help there.
----------------------------------------------

Good luck!
Jason Garber

Graham Dumpleton

unread,
Oct 12, 2009, 5:46:02 PM10/12/09
to mod...@googlegroups.com
As I said before, you may want to look at some ActiveX controls for Windows.

Also as I said before a number of times, and as Jason points out once
again, this is a Windows issue and I have suggested you find a more
appropriate list for the question where you will find people who know
about such issues.

So, perhaps now go back to StackOverflow now that you have a more
specific question which is really unrelated to mod_wsgi. Just divorce
your question from the fact you are using mod_wsgi and describe it in
terms of how can you from HTML/Javascript delivered to browser cause a
problem to be run on the same box as the browser. You will no doubt
get lots of people saying that you shouldn't do that, but see if the
SO folks can help all the same.

Graham

2009/10/13 Jason Garber <bo...@gahooa.com>:
Reply all
Reply to author
Forward
0 new messages