15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

172 self.langs.append(eachLang.value)<br />

173 else:<br />

174 self.langs.append(langdata.value)<br />

175 else:<br />

176 self.error = 'At least one language required.'<br />

177<br />

178 if form.has_key('upfile'):<br />

179 upfile = form["upfile"]<br />

180 self.fn = upfile.filename or ''<br />

181 if upfile.file:<br />

182 self.fp = upfile.file<br />

183 else:<br />

184 self.fp = StringIO('(no data)')<br />

185 else:<br />

186 self.fp = StringIO('(no file)')<br />

187 self.fn = ''<br />

188<br />

189 if not self.error:<br />

190 self.doResults()<br />

191 else:<br />

192 self.showError()<br />

193<br />

194 if __name__ == '__main__':<br />

195 page = AdvCGI()<br />

196 page.go()<br />

advcgi.py looks strikingly similar to our friends3.py CGI scripts seen earlier in this chapter. It has a<br />

form, results, and error pages to return. In addition to all of the advanced CGI features that are part of<br />

our new script, we are also using more of an object-oriented feel to our script by using a class with<br />

methods instead of just a set of functions. The HTML text for our pages is now static data for our class,<br />

meaning that they will remain constant across all instanceseven though there is actually only one<br />

instance in our case.<br />

Line-by-Line (Block-by-Block) Explanation<br />

Lines 17<br />

The usual startup and import lines appear here. The only module you may not be familiar with is<br />

cStringIO, which we briefly introduced at the end of Chapter 10 and also used in Example 20.1.<br />

cStringIO.StringIO() creates a file-like object out of a string so that access to the string is similar to<br />

opening a file and using the handle to access the data.<br />

Lines 912<br />

After the AdvCGI class is declared, the header and url (static class) variables are created for use by the<br />

methods displaying all the different pages.<br />

Lines 1480<br />

All the code in this block is used to generate and display the form page. The data attributes speak for<br />

themselves. getCPPCookies() obtains cookie information sent by the Web client, and showForm() collates

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

Saved successfully!

Ooh no, something went wrong!