Herramientas de usuario

Herramientas del sitio


fw:others:webpy

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
fw:others:webpy [2011/08/27 16:40]
alfred
fw:others:webpy [2020/05/09 09:25] (actual)
Línea 4: Línea 4:
  
 ===== Uso ===== ===== Uso =====
 +
  
  
Línea 42: 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 49: 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
 +
 +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 ==== ==== Subir ficheros ====
Línea 107: Línea 168:
  
 ===== Notas ===== ===== Notas =====
 +
  
  
Línea 115: Línea 177:
 === ... Usarlo en un servidor de producción?​ === === ... Usarlo en un servidor de producción?​ ===
 Tipo Apache o Lighttpd. \\  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>​
 +
fw/others/webpy.1314463216.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)