File Uploading with WebApp2 and without Google App Engine to local storage

If you are trying to use WebApp2 outside of Google App Engine in your own python projects, I’ve found a simple way to upload files to the directory of your running *.py file.

All you have to do is render a simple form, and send the POST to an ‘upload’ handler that reads the POST message and grabs the file out of it.

form:

<form action="/upload" enctype="multipart/form-data" method="post">
<input type="file" name="datafile" size="40">
<br>
<input type="submit" value="Send">
</form>

Python (inserted into the rest of your webapp2 program):

class Upload(Handler):
def post(self):
datafile = self.request.POST['datafile']
file_filename = datafile.filename
file_string = datafile.value
open(file_filename, 'wb').write(file_string)

and this saves the file into the directory that you are running the *.py file from.
Simple!

Here is a working example: Upload.zip

facebooktwittergoogle_pluslinkedin

Generating and Decoding QR Codes with Python on Windows

This is quite a work-around so far but I was able to interface python with QR codes.
I’m still in the process of writing it up, but I used the following:
Java 7 JDK – http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
(with included Java 7 Runtime JRE)
ZXing – https://code.google.com/p/zxing/
a ripped apart version of python-zxing – https://github.com/oostendo/python-zxing

facebooktwittergoogle_pluslinkedin

WebApp2 After Udacity / Google App Engine – Handling Other Files

A while ago, I took Udacity’s CS253 “Web Development” class.

I’m still a newb when it comes to programming and python, but have been practicing and trying out new things since finishing the class.

When I came up with a new idea for using Python, I would Google what I was trying to do and usually came across a python module related to my idea. It was pretty awesome that someone out there had already programmed a script to make my idea easier, but it was not awesome that Google App Engine only allows specific modules to be imported into their environment.

So I decided to break away from Google App Engine and try to get a server / application up and running on my own. I found the webapp2 module, installed it for Python 2.7 and found it to work great. It works just like it does in all the CS253 examples. The template looks something like this:


class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
params['user'] = self.user
return render_str(template, **params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))

class Main(Handler):
def get(self):
self.render('index.html')

class WhateverHandler(Handler)
def get(self):
self.render('index.html')

app = webapp2.WSGIApplication([
('/', Main),
('/whatever', WhateverHandler),
],
debug=True)

def main():
from paste import httpserver
httpserver.serve(app, host='0.0.0.0', port='8083')

if __name__ == '__main__':
main()

And that’s awesome and it works great and etc etc; but what happens when you want to put an image on your site? Good luck. I ran into a lot of problems, with the image not being found on the server and python interpreting jpgs and pngs as text and was trying to write that out to the browser.

Here is a quick solution for you beginner programmers like me. Lets just assume for now you are using JPGs only.

1) Create framework for image requests:
Create a new def in your ‘Class Handler(webapp2.RequestHandler): called “def image:”.
The problems I encountered were the browser seeing whatever JPG I was trying to send to it as a text file. We need to tell the browser that this is a JPG, and we do this with the line “ self.response.headers['Content-Type'] = ’image/jpeg’ ” which changes the ‘header’ of the message that the server is sending the browser.
Then we need to tell python to spit out the image, and we do this with the line “self.response.out.write(open(images_dir + filename + extension,”rb”).read())”
This breaks it out so we can tell python the directory the file is located in, the filename and the extension.
The final product looks like this:

class Handler(webapp2.RequestHandler):
def image(self, images_dir, filename, extension):
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(open(images_dir + filename + extension,"rb").read())

2) Add a new Handler just for image requests that uses image framework: images_dir is a predefined variable where the images are stored on your server

images_dir = 'img'

class ImageHandler(Handler):
def get(self, filename, extension):
self.image(templates_dir + '/' + images_dir, filename, extension)

3) Add a configuration line to route requests for images to the ImageHandler: FILE_RE is a ‘regular expression’ built to break files up into their name and extension

import re
FILE_RE = r'(.+?)(\.[^.]*$|$)'

app = webapp2.WSGIApplication([
('/', Main),
('/img' + FILE_RE , ImageHandler),
('/whatever', WhateverHandler),
],
debug=True)

def main():
from paste import httpserver
httpserver.serve(app, host='0.0.0.0', port='8083')

if __name__ == '__main__':
main()

4) Finally, we have

import re
import webapp2

FILE_RE = r'(.+?)(\.[^.]*$|$)'

class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
params['user'] = self.user
return render_str(template, **params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
def image(self, images_dir, filename, extension):
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(open(images_dir + filename + extension,"rb").read())

class Main(Handler):
def get(self):
self.render('index.html')

class WhateverHandler(Handler)
def get(self):
self.render('index.html')

class ImageHandler(Handler):
def get(self, filename, extension):
self.image(templates_dir + '/' + images_dir, filename, extension)

app = webapp2.WSGIApplication([
('/', Main),
('/img' + FILE_RE , ImageHandler),
('/whatever', WhateverHandler),
],
debug=True)

def main():
from paste import httpserver
httpserver.serve(app, host='0.0.0.0', port='8083')

if __name__ == '__main__':
main()

Let me know what you think.

facebooktwittergoogle_pluslinkedin

OSX 10.6.8 on Asus P5EVM-HDMI

I recently got OSX 10.6.8 working on an Asus P5EVM-HDMI using an Intel Q6600 with a NVIDIA GT220 Video card.

Built out of old components and running Snow Leopard 10.6.8, this thing is pretty awesome. I’m currently trying to figure out what to do with it / using it as a media center.

Specs are as follows:
Motherboard – Asus P5EVM HDMI
Processor – Intel Q6600 @ 2.4 GHz
RAM – 4GB ( 2 x 2GB) Corsair XMS2 @ 800mhz
Video: MSI GT220 512mb
DVD: ??? IDE Drive

Installation Process:
1) Get a copy of the OSX Snow Leopard Retail Installation DVD.
2) Go here, to tonymacx86′s blog and read his great Snow Leopard Installation Guide. You’ll also need to download iBoot and MultiBeast from his site, which requires registration. Burn the iBoot iso to a CD / DVD. Put the MultiBeast zip on a flash drive.
2a) Download the 10.6.8 update and put it on the same flash drive.
2b) Download the X zip for ethernet from the iats project page and put it on the same flash drive.
2c) Download the VoodooHDA 2.7.4 zip for audio from the OSx86.net downloads page and put it on the same flash drive.
2a) Start your machine and access the BIOS on the P5EVM HDMI. For my installation, I deactivated the following: C1E Support, Speedstep, Repost Video
3) Follow tonymacx86′s guide. I did NOT need to use any boot flags (ex, -x, -v, PCIRootUID=1) to get into the OSX installer. Format and partition the hard drive and install OSX. It should take about 20 minutes. When the machine restarts, you’ll have to put the iBoot disk back into the drive. Choose to load your newly installed OSX. Once the machine loads, install the 10.6.8 update from your flash drive.
4)  When you reach the point of installing MultiBeast (after installing the 10.6.8 update but BEFORE restarting, only install the following options:
- OSx86 Software – KextHelper
- OSx86 Software – KextWizard
Everything else needed will install on it’s own. MultiBeast will install the NVIDIA drivers for you and we will deal with audio and LAN next.
5) Restart the machine.
6) Using Kext Helper, install the Atheros ethernet kexts. Restart.
7) Install the Voodoo 2.7.4 package. Restart.
8) Choose your audio connection from System Preferences – Sound.
9) That’s it! Enjoy!

I still have to deal with the sleep issue but for now; I just shut the computer down when not in use. I will update this with how to resolve the sleep issue.

HDMI Audio works through the GT220 using the Voodoo 2.7.4 package! I use HDMI Audo Device #2 in System Preferences – Sound

facebooktwittergoogle_pluslinkedin