16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 25 ■ PROJECT 6: REMOTE EDITING WITH CGI 475<br />

text = form.getvalue('text', open('simple_edit.dat').read())<br />

f = open('simple_edit.dat', 'w')<br />

f.write(text)<br />

f.close()<br />

print """Content-type: text/html<br />

<br />

<br />

A Simple Edi<strong>to</strong>r<br />

<br />

<br />

<br />

%s<br />

<br />

<br />

<br />

<br />

""" % text<br />

When accessed through a Web server, the CGI script checks for an input value called text.<br />

If such a value is submitted, the text is written <strong>to</strong> the file simple_edit.dat. The default value is<br />

the file’s current contents. Finally, a Web page (containing the field for editing and submitting<br />

the text) is shown. A screenshot of this page is shown in Figure 25-1.<br />

Figure 25-1. The simple_edit.cgi script in action

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!