Python Tutorial ( PDFDrive )
# Get filename here.fileitem = form['filename']# Test if the file was uploadedif fileitem.filename:# strip leading path from file name to avoid# directory traversal attacksfn = os.path.basename(fileitem.filename)open('/tmp/' + fn, 'wb').write(fileitem.file.read())message = 'The file "' + fn + '" was uploaded successfully'else:message = 'No file was uploaded'print """\Content-Type: text/html\n<html><body><p>%s</p></body></html>""" % (message,)If you are running above script on Unix/Linux, then you would have to take care of replacing file separator asfollows, otherwise on your windows machine above open() statement should work fine.fn = os.path.basename(fileitem.filename.replace("\\", "/" ))How To Raise a "File Download" Dialog Box? Sometimes, it is desired that you want to give option where a user will click a link and it will pop up a "FileDownload" dialogue box to the user instead of displaying actual content. This is very easy and will be achievedthrough HTTP header. This HTTP header will be different from the header mentioned in previous section.For example,if you want make a FileName file downloadable from a given link, then its syntax will be as follows:#!/usr/bin/python# HTTP Headerprint "Content-Type:application/octet-stream; name=\"FileName\"\r\n";print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";# Actual File Content will go hear.fo = open("foo.txt", "rb")str = fo.read();print str# Close opened filefo.close()Hope you enjoyed this tutorial. If yes, please send me your feedback at: Contact UsTUTORIALS POINT Simply Easy Learning
CHAPTER21Python Database AccessThe Python standard for database interfaces is the Python DB-API. Most Python database interfacesadhere to this standard.You can choose the right database for your application. Python Database API supports a wide range of databaseservers:• GadFly• mSQL• MySQL• PostgreSQL• Microsoft SQL Server 2000• Informix• Interbase• Oracle• SybaseHere is the list of available Python database interfaces: Python Database Interfaces and APIs .You mustdownload a separate DB API module for each database you need to access. For example, if you need to accessan Oracle database as well as a MySQL database, you must download both the Oracle and the MySQL databasemodules.The DB API provides a minimal standard for working with databases using Python structures and syntaxwherever possible. This API includes the following:• Importing the API module.• Acquiring a connection with the database.TUTORIALS POINT Simply Easy Learning
- Page 287 and 288: Syntax Following is the syntax for
- Page 289 and 290: Return Value This method does not
- Page 291 and 292: # stat informationstatinfo = os.sta
- Page 293 and 294: Description The method tcgetpgrp()
- Page 295 and 296: os.tempnam(dir, prefix)Parameters
- Page 297 and 298: Description The method ttyname() re
- Page 299 and 300: Return Value This method does not
- Page 301 and 302: Return Value This method returns t
- Page 303 and 304: KeyErrorNameErrorUnboundLocalErrorE
- Page 305 and 306: ......................else:If there
- Page 307 and 308: print "Error: can\'t find file or r
- Page 309 and 310: self.args = argSo once you defined
- Page 311 and 312: 'Optional class documentation strin
- Page 313 and 314: For the above class let's try to ac
- Page 315 and 316: print 'Calling child method'c = Chi
- Page 317 and 318: Data Hiding: An object's attribute
- Page 319 and 320: groups()This method returns all mat
- Page 321 and 322: SYNTAX: re.sub(pattern, repl, strin
- Page 323 and 324: \s Matches whitespace. Equivalent t
- Page 325 and 326: Backreferences: This matches a prev
- Page 327 and 328: CHAPTER20Python CGI ProgrammingWhat
- Page 329 and 330: If you click hello.py, then this pr
- Page 331 and 332: Simple URL Example : Get Metho
- Page 333 and 334: </form>The result of this code is t
- Page 335 and 336: text_content = form.getvalue('textc
- Page 337: Here is an example of how to retrie
- Page 341 and 342: EXAMPLE: Following is the example o
- Page 343 and 344: ('Mac', 'Mohan', 20, 'M', 2000)try:
- Page 345 and 346: DELETE Operation: DELETE operation
- Page 347 and 348: ProgrammingErrorSubclass of Databas
- Page 349 and 350: The socket Module: To create a so
- Page 351 and 352: Protocol Common function Port No Py
- Page 353 and 354: Subject: SMTP e-mail testThis is a
- Page 355 and 356: try:smtpObj = smtplib.SMTP('localho
- Page 357 and 358: count = 0while count < 5:time.sleep
- Page 359 and 360: Synchronizing Threads: The threadi
- Page 361 and 362: thread = myThread(threadID, tName,
- Page 363 and 364: <stars>10</stars><description>Talk
- Page 365 and 366: print "Rating:", self.ratingelif se
- Page 367 and 368: Format: DVDRating: PGDescription: T
- Page 369 and 370: Tkinter Widgets Tkinter provides v
- Page 371 and 372: fgfontheighthighlightcolorimagejust
- Page 373 and 374: WidthXscrollincrementXscrollcommand
- Page 375 and 376: pattern when it is over the checkbu
- Page 377 and 378: The Entry widget is used to accept
- Page 379 and 380: E1 = Entry(top, bd =5)E1.pack(side
- Page 381 and 382: Optionanchorbgbitmapbdcursorfontfgh
- Page 383 and 384: selectmodeDetermines how many items
- Page 385 and 386: Syntax: Here is the simple syntax t
- Page 387 and 388: Menu The goal of this widget is to
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid
# directory traversal attacks
fn = os.path.basename(fileitem.filename)
open('/tmp/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html>
<body>
<p>%s</p>
</body>
</html>
""" % (message,)
If you are running above script on Unix/Linux, then you would have to take care of replacing file separator as
follows, otherwise on your windows machine above open() statement should work fine.
fn = os.path.basename(fileitem.filename.replace("\\", "/" ))
How To Raise a "File Download" Dialog Box?
Sometimes, it is desired that you want to give option where a user will click a link and it will pop up a "File
Download" dialogue box to the user instead of displaying actual content. This is very easy and will be achieved
through HTTP header. This HTTP header will be different from the header mentioned in previous section.
For example,if you want make a FileName file downloadable from a given link, then its syntax will be as follows:
#!/usr/bin/python
# HTTP Header
print "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";
# Actual File Content will go hear.
fo = open("foo.txt", "rb")
str = fo.read();
print str
# Close opened file
fo.close()
Hope you enjoyed this tutorial. If yes, please send me your feedback at: Contact Us
TUTORIALS POINT
Simply Easy Learning