Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
fw:others:webpy [2011/08/08 17:43] alfred |
fw:others:webpy [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 3: | Línea 3: | ||
| * [[http://webpy.org/docs/0.3/|Documentación]] | * [[http://webpy.org/docs/0.3/|Documentación]] | ||
| - | ===== Básico ===== | + | ===== Uso ===== |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| Línea 48: | Línea 43: | ||
| + | |||
| + | |||
| + | |||
| + | ==== Tratar la entreda ==== | ||
| + | === Recoger datos del POST === | ||
| + | * ''web.input()'' devuelve los datos pasados por POST. | ||
| + | <code python> | ||
| + | web.input() # Devuelve lista de parámetros. | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ==== Tratar la salida ==== | ||
| + | === Cambiar el content-type === | ||
| + | <code python> | ||
| + | class index: | ||
| + | def GET(self, code): | ||
| + | web.header('Content-Type', 'text/xml') | ||
| + | return render.index(code) | ||
| + | </code> | ||
| + | Puedes ver distintos content-type [[code:concepts#protocolo_http|aquí]]. | ||
| + | === Devolver un fichero === | ||
| + | <code python> | ||
| + | def GET(self, code): | ||
| + | ... | ||
| + | web.header('Content-Type', 'text/csv') | ||
| + | web.header('Content-Disposition', 'attachment;filename=myfilename.csv') | ||
| + | return value | ||
| + | </code> | ||
| Línea 55: | Línea 78: | ||
| - | ==== Sesiones ==== | + | |
| + | ==== Sesiones y autentificación ==== | ||
| [[http://webpy.org/docs/0.3/sessions]] | [[http://webpy.org/docs/0.3/sessions]] | ||
| + | |||
| + | |||
| + | |||
| ==== Contenido estático ==== | ==== Contenido estático ==== | ||
| + | === Carpeta static === | ||
| + | Podemos agregar una carpeta static en la ruta de la aplicación, esta contendrá los documentos que no cambian. Por ejemplo ''<nowiki>http://localhost/static/logo.png</nowiki>'' enviaría la imágen ''./static/logo.png''. \\ | ||
| + | Una carpeta mapeada a partir de la directiva ''Alias'' de Apache también serviría. | ||
| + | === A partir de código === | ||
| + | <code python> | ||
| + | import os | ||
| + | import web | ||
| - | ===== Avanzado ===== | + | urls = ( |
| + | '/images/(.*)', 'images' #this is where the image folder is located.... | ||
| + | ) | ||
| + | class images: | ||
| + | def GET(self,name): | ||
| + | ext = name.split(".")[-1] # Gather extension | ||
| + | cType = { | ||
| + | "png":"images/png", | ||
| + | "jpg":"image/jpeg", | ||
| + | "gif":"image/gif", | ||
| + | "ico":"image/x-icon" } | ||
| + | |||
| + | if name in os.listdir('images'): # Security | ||
| + | web.header("Content-Type", cType[ext]) # Set the Header | ||
| + | return open('images/%s'%name,"rb").read() # Notice 'rb' for reading images | ||
| + | else: | ||
| + | raise web.notfound() | ||
| + | </code> | ||
| + | |||
| + | ==== Subir ficheros ==== | ||
| + | |||
| + | ===== Avanzado ===== | ||
| Línea 67: | Línea 122: | ||
| * [[http://code.google.com/p/mimerender|Página web]] | * [[http://code.google.com/p/mimerender|Página web]] | ||
| * {{fw:others:mimerender-0.2.2.tar.gz|}} | * {{fw:others:mimerender-0.2.2.tar.gz|}} | ||
| - | ===== Cómo... ===== | + | Es una librería que parsea el retorno de una URL en el formato indicado. |
| + | === Código de ejemplo === | ||
| + | <code python> | ||
| + | import web | ||
| + | import json | ||
| + | from mimerender import mimerender | ||
| - | ==== ... subir archivos? ==== | + | render_xml = lambda message: '<message>%s</message>'%message |
| + | render_json = lambda **args: json.dumps(args) | ||
| + | render_html = lambda message: '<html><body>%s</body></html>'%message | ||
| + | render_txt = lambda message: message | ||
| - | ==== Mini-comos ==== | + | urls = ( |
| - | === Usar Cheetah === | + | '/(.*)', 'greet' |
| - | === Usar Elixir === | + | ) |
| + | app = web.application(urls, globals()) | ||
| + | |||
| + | class greet: | ||
| + | @mimerender( | ||
| + | default = 'html', | ||
| + | html = render_html, | ||
| + | xml = render_xml, | ||
| + | json = render_json, | ||
| + | txt = render_txt | ||
| + | ) | ||
| + | def GET(self, name): | ||
| + | if not name: | ||
| + | name = 'world' | ||
| + | return {'message': 'Hello, ' + name + '!'} | ||
| + | |||
| + | if __name__ == "__main__": | ||
| + | app.run() | ||
| + | </code> | ||
| + | === Llamadas y retorno === | ||
| + | <code> | ||
| + | $ curl -H "Accept: application/html" localhost:8080/x | ||
| + | <html><body>Hello, x!</body></html> | ||
| + | $ curl -H "Accept: application/xml" localhost:8080/x | ||
| + | <message>Hello, x!</message> | ||
| + | $ curl -H "Accept: application/json" localhost:8080/x | ||
| + | {'message':'Hello, x!'} | ||
| + | $ curl -H "Accept: text/plain" localhost:8080/x | ||
| + | Hello, x! | ||
| + | </code> | ||
| ===== Notas ===== | ===== Notas ===== | ||
| + | |||
| + | |||
| + | |||
| + | ==== Cómo... ==== | ||
| + | === ... Usar Cheetah? === | ||
| + | === ... Usar Elixir? === | ||
| + | Para utilizar [[fw:elixir|Elixir]] en un código que utilice web.py simplemente has de lanzar un ''setup_all()'' al principio, justo después de haber importado el código del modelo. | ||
| + | === ... Usarlo en un servidor de producción? === | ||
| + | Tipo Apache o Lighttpd. \\ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ==== Notas ==== | ||
| + | === Aplicaciones de ejemplo === | ||
| + | * {{fw:others:webpy:andreisavu-music-share-09b9eec.zip|Aplicación para compartir .mp3}} | ||
| + | === Ideas para la creación de un proceso en background === | ||
| + | :!: Falta probar en producción | ||
| + | <code python> | ||
| + | #!/usr/bin/python | ||
| + | # -*- coding: utf-8 *-* | ||
| + | |||
| + | import web | ||
| + | import json | ||
| + | from mimerender import mimerender | ||
| + | from multiprocessing import Process | ||
| + | |||
| + | render_xml = lambda message: '<message>%s</message>'%message | ||
| + | render_json = lambda **args: json.dumps(args) | ||
| + | render_html = lambda message: '<html><body>%s</body></html>'%message | ||
| + | render_txt = lambda message: message | ||
| + | |||
| + | urls = ( | ||
| + | '/(.*)', 'greet' | ||
| + | ) | ||
| + | app = web.application(urls, globals()) | ||
| + | p = Process() | ||
| + | |||
| + | class greet: | ||
| + | @mimerender( | ||
| + | default = 'html', | ||
| + | html = render_html, | ||
| + | xml = render_xml, | ||
| + | json = render_json, | ||
| + | txt = render_txt | ||
| + | ) | ||
| + | def GET(self, name): | ||
| + | global p | ||
| + | p = Process(target = self.test) | ||
| + | p.start() | ||
| + | if not name: | ||
| + | name = 'world' | ||
| + | return {'message': 'Hello, ' + name + '!'} | ||
| + | | ||
| + | def test (self): | ||
| + | import os | ||
| + | print 'entra!!!' | ||
| + | os.system('ls -laR / > /home/alfred/Desktop/test.txt') | ||
| + | |||
| + | if __name__ == "__main__": | ||
| + | app.run() | ||
| + | p.join() | ||
| + | </code> | ||