Diferència entre revisions de la pàgina «Python»
		
		
		
		
		
		
		Salta a la navegació
		Salta a la cerca
		
				
		
		
		
		
		
		
		
	
| (Hi ha 4 revisions intermèdies del mateix usuari que no es mostren) | |||
| Línia 25: | Línia 25: | ||
| == Més Python == | == Més Python == | ||
| + | * Per generar passwords aleatòries: http://stackoverflow.com/questions/2257441/python-random-string-generation-with-upper-case-letters-and-digits | ||
| * Per activar el debug per la pantalla del navegador enlloc de /var/log/apache2/error.log: | * Per activar el debug per la pantalla del navegador enlloc de /var/log/apache2/error.log: | ||
| *:http://code.google.com/p/modwsgi/wiki/DebuggingTechniques#Error_Catching_Middleware | *:http://code.google.com/p/modwsgi/wiki/DebuggingTechniques#Error_Catching_Middleware | ||
| Línia 38: | Línia 39: | ||
| **Authentication: http://sluggo.scrapping.cc/python/Akhet/auth.html | **Authentication: http://sluggo.scrapping.cc/python/Akhet/auth.html | ||
| **... | **... | ||
| − | *  | + | * Python i DDBB: | 
| − | *:http://www.kitebird.com/articles/pydbapi.html | + | ** MySQL: http://www.kitebird.com/articles/pydbapi.html | 
| + | ** MongoDB: Pymongo | ||
| + | ** AWS (SimpleDB, DynamoDB): [http://boto.cloudhackers.com/en/latest/index.html boto library] | ||
| + | ** ... | ||
| * Cookies: | * Cookies: | ||
| ** http://www.jayconrod.com/posts/17/how-to-use-http-cookies-in-python | ** http://www.jayconrod.com/posts/17/how-to-use-http-cookies-in-python | ||
| * Persistència d'objectes: | * Persistència d'objectes: | ||
| − | **Amb pickle: http://docs.python.org/library/pickle.html | + | ** Amb pickle: http://docs.python.org/library/pickle.html | 
| − | ** | + | ** Shelves | 
| + | ** ZODB | ||
| *GUI: hi ha diverses opcions: | *GUI: hi ha diverses opcions: | ||
| **wxPython: http://wxpython.org/ Té editors visuals com wxDesigner o wxGlade | **wxPython: http://wxpython.org/ Té editors visuals com wxDesigner o wxGlade | ||
| Línia 56: | Línia 61: | ||
| == Web services: Cornice == | == Web services: Cornice == | ||
| Package: Cornice | Package: Cornice | ||
| + | |||
| + | Abans de començar cal crear un nou projecte amb Pyramid ([[Buildout]]) i cal afegir la '''llibreria cornice''' al '''buildout.cfg'''. | ||
| + | |||
| * Treballarem amb JSON: [http://en.wikipedia.org/wiki/JSON wikipedia] i [http://www.json.org/ json.org]. Java utilitza XMLRPC, i Microsfot SOAP. | * Treballarem amb JSON: [http://en.wikipedia.org/wiki/JSON wikipedia] i [http://www.json.org/ json.org]. Java utilitza XMLRPC, i Microsfot SOAP. | ||
| * Doc.oficial: http://packages.python.org/cornice/ | * Doc.oficial: http://packages.python.org/cornice/ | ||
| Línia 70: | Línia 78: | ||
| == Criptografia == | == Criptografia == | ||
| + | * Crypt: (one-way) http://docs.python.org/library/crypt.html | ||
| * PyCrypto: http://pypi.python.org/pypi/pycrypto/ | * PyCrypto: http://pypi.python.org/pypi/pycrypto/ | ||
| ** Doc original: http://packages.python.org/pycrypto/ | ** Doc original: http://packages.python.org/pycrypto/ | ||
| ** Exemple molt clarificador: http://www.laurentluce.com/posts/python-and-cryptography-with-pycrypto/ | ** Exemple molt clarificador: http://www.laurentluce.com/posts/python-and-cryptography-with-pycrypto/ | ||
| ** Per gravar la key en fitxer: http://stackoverflow.com/questions/9197507/saving-rsakeys-to-a-file-using-pycrypto | ** Per gravar la key en fitxer: http://stackoverflow.com/questions/9197507/saving-rsakeys-to-a-file-using-pycrypto | ||
| + | * Pot ser útil la llibreria base64 per codificar el resultat xifrat en caràcters compatibles ASCII i poder-ho passar per HTTP (JSON o el què sigui). | ||
| + | ** Base64 lib: http://docs.python.org/library/base64.html | ||
| * ... | * ... | ||
Revisió de 12:17, 7 maig 2013
Python és un llenguatge interpretat i completament orientat a objectes. Les seves principals aplicacions son:
- Aplicacions web
- Aplicacions d'escriptori (com GNOME, etc.)
Google ha impulsat molt la seva utilització. És més segur que altres llenguatges com PHP.
Contingut
Primeres passes amb Python[modifica]
...
Instal·lació en Apache2 amb mòdul WSGI[modifica]
Per poder utiltizar Python en Apache2 es pot fer de 2 maneres:
- Com a script CGI i amb el mòdul libapache2-mod-python (desaconsellat)
- Com a script WSGI, que és el standard per Python. Els detalls a continuació son per aquesta versió.
Per tant:
- Instal·la el mòdul WSGI
- Segons el fòrum anterior s'instal·la de manera que els scripts s'executen si tenen l'extensió .wsgi
- Pots afegir l'extensió .py alhora que la .wsgi per executar scripts Python en Apache.
 
- Segueix el tutorial per posar en marxa la primera aplicació Python-WSGI
- http://webpython.codepoint.net/wsgi_tutorial
- En aquest tutorial es posa en marxa el servidor des de la línia de comandes. El entorn wsgi ens permet executar l'aplicació mitjançant un servidor httpd propi (sense Apache) i accedint a un port particular. Si vols executar-ho en l'entorn Apache has de treure la crida a httpd.handle_request o httpd.serve_forever i posar-ho en un directori visible per l'Apache (/var/www o com ho tinguis configurat).
 
Més Python[modifica]
- Per generar passwords aleatòries: http://stackoverflow.com/questions/2257441/python-random-string-generation-with-upper-case-letters-and-digits
- Per activar el debug per la pantalla del navegador enlloc de /var/log/apache2/error.log:
- Cal tenir en compte que en mode de producció no es pot tenir el debug activat. Tocant la conf de l'Apache es pot arreglar:
 
- Pyramid
- Virtualenv
- First Pyramid app: http://docs.pylonsproject.org/projects/pyramid/en/1.2-branch/narr/firstapp.html#firstapp-chapter
- Primer projecte Pyramid: http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/project.html?awesome
- WSGI integració de Pyramid (Pylons) amb Apache: http://code.google.com/p/modwsgi/wiki/InstallationInstructions
 
- Més Pyramid:
- Python i DDBB:
- MySQL: http://www.kitebird.com/articles/pydbapi.html
- MongoDB: Pymongo
- AWS (SimpleDB, DynamoDB): boto library
- ...
 
- Cookies:
- Persistència d'objectes:
- Amb pickle: http://docs.python.org/library/pickle.html
- Shelves
- ZODB
 
- GUI: hi ha diverses opcions:
- wxPython: http://wxpython.org/ Té editors visuals com wxDesigner o wxGlade
- PyQt: http://zetcode.com/tutorials/pyqt4/firstprograms/ Té editor visual QtDesigner.
- ...
 
- Àudio:
- Snack sound (necessita també Tcl/Tk): http://www.speech.kth.se/snack/
- Pygame: http://www.pygame.org/
- ...
 
Web services: Cornice[modifica]
Package: Cornice
Abans de començar cal crear un nou projecte amb Pyramid (Buildout) i cal afegir la llibreria cornice al buildout.cfg.
- Treballarem amb JSON: wikipedia i json.org. Java utilitza XMLRPC, i Microsfot SOAP.
- Doc.oficial: http://packages.python.org/cornice/
- Interessant i essencial el "Quick Start for people in a hurry" (emmagatzema objectes JSON), però hi ha un error: on diu _VALUES.set(...) en realitat ha de ser:_VALUES[key] = json.loads(request.body) 
- També necessitarem CURL (amb apt-get). Consulta el man o també online: http://curl.haxx.se/docs/manpage.htm
- OJU amb JSON i curl, les cometes han de ser simples per tot el objecte JSON i dobles pels elements:$ curl http://el/meu/servei -d '{"nom1":"valor1",...}'
- Més opcions curl: -X {GET|POST|PUT}
 
- OJU amb JSON i curl, les cometes han de ser simples per tot el objecte JSON i dobles pels elements:
- Llibreria PycURL: http://pycurl.sourceforge.net/ ens permet fer crides des de python amb cURL.
- Oju: s'ha d'instal·lar al sistema (o al virtualenv) i cal afegir prèviament els paquets: python2.?-dev i libcurl4-gnutls-dev
- Després ja es pot instal·lar amb $ easy_install pycurl 
 
- Millor que pycurl és urllib que ja està al sistema i per fer el fet anirem més ràpids
- ...
Criptografia[modifica]
- Crypt: (one-way) http://docs.python.org/library/crypt.html
- PyCrypto: http://pypi.python.org/pypi/pycrypto/
- Doc original: http://packages.python.org/pycrypto/
- Exemple molt clarificador: http://www.laurentluce.com/posts/python-and-cryptography-with-pycrypto/
- Per gravar la key en fitxer: http://stackoverflow.com/questions/9197507/saving-rsakeys-to-a-file-using-pycrypto
 
- Pot ser útil la llibreria base64 per codificar el resultat xifrat en caràcters compatibles ASCII i poder-ho passar per HTTP (JSON o el què sigui).
- Base64 lib: http://docs.python.org/library/base64.html
 
- ...

