Take the 2-minute tour ×

I am making a web server using webpy and right now I have a simple form and a special class I wrote. I want the user to be able to write in code to the form and if the input is a function call, to call the function the user submitted, else do anything with the input if it's not a function call. I believe this would have to be achieved somewhere in the template file? any help would be appreciate

share|improve this question

2 Answers 2

up vote 3 down vote accepted

If I understand correctly, you're looking for a way to run client-submitted code on the server, getting input from a form.

So assuming you have a form template that looks something like this (note the name of the textarea element):

<form method="POST">
<textarea name="code" />
<input type="submit">

you will want to add a POST method to your page class to handle the input and run the code:

class MyPage:
    def POST(self):
        data = web.input()

This is not something you should do with a form that is accessible to anybody you don't trust completely, as it gives the ability to run any code they want on your web-server (e.g. they could easily try and delete all the files on the server). But if you're ok with the security risks or just playing around, be sure to read the docs for Pythons eval to see the other arguments you can pass to it.

share|improve this answer
I think I'm running into issues with how my form is implemented, using webpy I render some of the form on the server: my_form = web.form.Form(web.form.Textbox('code',class_='code',id='code') and in the template: <form class="form" method="POST"> $:form.render() <input class="button" type="submit" value="send"/> </form> the first parameter of the web.form object should be the forms name but when I call data.code I get an attribute error : code –  langor Jul 27 '12 at 16:59
Nevermind I got access to the form now I'm just running into issues of syntax and using eval with webpy –  langor Jul 27 '12 at 19:47

Create a Python cgi script and run it in cgi-bin of your webserver. Here is a tutorial. Then the script can access the form's input and do whatever you want with it.

share|improve this answer
The cgi module can also be very helpful, especially in accessing forms. –  user1557602 Jul 27 '12 at 15:53

Your Answer


