2

I finished up some python tutorials and would like to go a bit further. I can open the IDLE and execute the code just fine by pressing f5 (save and run) on my desktop but that is the limit of my abilities. I would like to be able to execute the programs on a webpage

I tried simply uploading the file to my server, then browsing to it in chrome. I'm sure you know what happened: the url displayed text on the screen.

Since I am brand new to python, I am not sure where to start or even what questions to ask. Basically I would like to run the program in the browswer as if the browswer was the IDLE, or better yet, create an html/css button that runs the program when clicked.

tshepang
  • 12,111
  • 21
  • 91
  • 136
tora0515
  • 2,479
  • 12
  • 33
  • 40
  • 2
    you can run it as CGI I think ... you can use php's exec function. you can use flask or django and run a website with fcgi or passenger. it depends on what you mean ...but I think just using php's exec function is maybe what you want – Joran Beasley Apr 10 '13 at 04:06
  • @Joran Thanks, I will start with the exec function and see what I get. Basically I would just like a static webpage that has some "click me to run the simple python script" button. – tora0515 Apr 10 '13 at 04:13
  • make sure your py file starts with `#!/bin/python` and/or call it as `exec('python myscript.py')` ... also this is probably a big huge gaping security risk so I would keep the URL secret and maybe password protected ... really I would recommend doing this on a localhost only maybe – Joran Beasley Apr 10 '13 at 04:15
  • is CGI any more secure than exec()? – tora0515 Apr 10 '13 at 04:20
  • not necessarily, but I dont know about all the intricacies of web stacks – Joran Beasley Apr 10 '13 at 04:21
  • that's a shame. Well, since I have been trying to learn python, I guess CGI would be better for me to look into anyway. I do have a local server set up, so I will practice there. Thank you – tora0515 Apr 10 '13 at 04:22
  • possible duplicate of [Executing a Python script in Apache2](http://stackoverflow.com/questions/9145517/executing-a-python-script-in-apache2) – tripleee Apr 10 '13 at 04:52

4 Answers4

5

I'd advise you to look into something like flask. It's a micro-framework that includes a basic web server. The documentation should get you most of the way that you want to go.

Running python on the web usually means that when you hit an URL, the server runs some kind of python script, and returns a string - the HTML content of the page you're requesting. You can't use python to 'script' the webpage as you'd use javascript.

You can run python in an interactive interpreter running within a webpage though - just check out try ipython.

Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
2

Flask is good.

Cherrypy - http://www.cherrypy.org/ - is also a great choice for a really simple way to run python on the server.

Leopd
  • 41,333
  • 31
  • 129
  • 167
2

Fundamentally, you just configure your web server to execute the file instead of display it. Typically you set this for *.py files but you could restrict it, say, to files in a particular directory. Apparently, your server already has such a setting for PHP files.

Wrapping Python with PHP (obviously) adds neither speed nor security or utility.

Down the line you might want to look at frameworks, mod_python, WSGI, etc, but for your immediate problem, those are severe overkill.

This is limited to static server-side code; JavaScript runs interactively in the visitor's browser, allowing for much richer user interaction. A server-side script runs when the browser attempts to load the page, and the page load finishes when the script is done. If you want something like IDLE in your browser, that's a JavaScript challenge rather than a Python task (and that particular wheel has already been invented, productized, marketed, and sold to the Americans: http://pythonfiddle.com/)

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

like this:

python -c "import urllib2; exec urllib2.urlopen("http://localhost:8000/test.py").read()"
vissviews
  • 21
  • 2