pour ne pas bufferiser l'ouput d'un script python et avoir le résultat en direct dans un | tee -a :
python -u script.py | tee -a mon.log
Good to know because it hurts me so bad:
When doing a put/post request with requests library, if the response is a redirect, request do the request again BUT with GET method...
-,-
python -m SimpleHTTPServer 8000
python -m http.server 8000
def print_format_table():
"""
prints table of formatted text format options
"""
for style in range(8):
for fg in range(30,38):
s1 = ''
for bg in range(40,48):
format = ';'.join([str(style), str(fg), str(bg)])
s1 += '\x1b[%sm %s \x1b[0m' % (format, format)
print(s1)
print('\n')
print_format_table()
python3 log to file AND stdout
import logging
logging.basicConfig(handlers=[logging.FileHandler('/var/log/runner/process1.log'),logging.StreamHandler()],format='%(asctime)s %(levelname)s %(message)s',level=logging.INFO)
logging.info('foo')
Encore mieux pour supporter le logrotate sans copytruncate :
import logging.handlers
logging.basicConfig(handlers=[logging.handlers.WatchedFileHandler('/var/log/worker/worker1.log'),logging.StreamHandler()],format='%(asctime)s %(levelname)s %(message)s',level=logging.INFO)
/var/log/worker/*.log {
monthly
rotate 12
compress
delaycompress
missingok
notifempty
create 644 root root
}
Python 2:
import logging as loggingg
logging = loggingg.getLogger('simple_example')
logging.setLevel(loggingg.INFO)
formatter = loggingg.Formatter('%(asctime)s %(levelname)s %(message)s')
console_handler = loggingg.StreamHandler()
console_handler.setLevel(loggingg.INFO)
console_handler.setFormatter(formatter)
file_handler = loggingg.FileHandler('/var/log/worker/worker3.log')
file_handler.setLevel(loggingg.INFO)
file_handler.setFormatter(formatter)
logging.addHandler(console_handler)
logging.addHandler(file_handler)
import pprint
p = pprint.PrettyPrinter(indent=4)
p.pprint(x)
or
import pprint
pprint.pformat(x)
import logging
import pprint
logging.info(pprint.pformat(dict))
$ cat monitor_beanstalk.py
import boto3
import pprint
pp = pprint.PrettyPrinter(indent=4)
client = boto3.client('elasticbeanstalk')
envs = client.describe_environments()['Environments']
for env in envs:
print 'ApplicationName: {} EnvironmentName: {} Health: {} HealthStatus: {} Status: {}'.format(env['ApplicationName'].ljust(30),env['EnvironmentName'].ljust(30),env['Health'].ljust(10),env.get('HealthStatus', 'N/A').ljust(10),env['Status'].ljust(10))
if (env['Health'] != 'Green') or (env.get('HealthStatus', 'N/A') != 'Ok' and env.get('HealthStatus', 'N/A') != 'N/A'):
print '\nProblem'
details = client.describe_environment_health(EnvironmentName=env['EnvironmentName'],AttributeNames=['All'])
#pp.pprint(details)
print details['Causes']
print details['InstancesHealth']
print '\n'
for root, dirs, files in os.walk("folder"):
for file in files:
if file.endswith(".yml"):
print(os.path.join(root, file))
with open(os.path.join(root, file), "r") as sources:
lines = sources.readlines()
with open(os.path.join(root, file), "w") as sources:
for line in lines:
sources.write(re.sub(r'pattern', 'foo', line))
Add /json to pypi package url.. Magic!
catching all exception in python
import traceback
for record in database:
try:
blabla
except (KeyboardInterrupt, SystemExit):
raise
except Exception as e:
# report error and proceed
print(type(e).name)
print(e)
print(traceback.format_exc())
Ne pas oublier de raise si on veut re-raise
subnets = ec2.subnets.all()
subnets_sorted = sorted(subnets, key=lambda k: k.tags[next(index for (index, d) in enumerate(k.tags) if d["Key"] == "Name")]['Value'])
Well, my python level is not good enough to clearly understand this but my Google level was largely enough to build this
Running Bottle with a different server
As said above, the standard server is perfectly suitable for development, personal use or a small group of people only using your application based on Bottle. For larger tasks, the standard server may become a bottleneck, as it is single-threaded, thus it can only serve one request at a time.
But Bottle has already various adapters to multi-threaded servers on board, which perform better on higher load. Bottle supports Cherrypy, Fapws3, Flup and Paste.
If you want to run for example Bottle with the Paste server, use the following code:
from bottle import PasteServer
...
run(server=PasteServer)
This works exactly the same way with FlupServer, CherryPyServer and FapwsServer.
import logging
logging.basicConfig(filename='log.txt', format=logging.BASIC_FORMAT)
logging.error('OH NO!')
try:
raise Exception('Foo')
except:
logging.exception("Oops:")
Un cours en français qui a l'air très bien. Via sametmax sur twitter
useful script to migrate redis server without downtime
+ http://boomboomboom.biz/blog/2013/09/07/migrate-redis-to-a-new-server-without-downtime/
Pratique pour avoir des stat rapidement dans ses scripts python (si on n'a pas de stack graphite sous la main)
How to pretty print a json string :
cat pref.json | python -m json.tool
for uuoc nazi ;) :
< pref.json python -m json.tool
or
python -m json.tool < pref.json
Pour le fun et le learning : faire du tcp avec python / scapy
via sametmax
mini web framework en python, dans le meme genre que bottle :
http://bottlepy.org/docs/dev/index.html
une lib pour interagir facilement avec bash
Un serveur http wsgi en python, pour faire tourner du django. A voir si les perf sont meilleure qu'avec le mod apache
L'équivalent de php-fpm pour python (mettre un nginx devant)
Twisted is an event-driven networking engine written in Python and licensed under the open source MIT license.
virtualenv is a tool to create isolated Python environments.
Librairie python qui va nous servir pour piclodio2 (https://github.com/Sispheor/Piclodio2)
Chaque réveil est en fait une ligne dans le cron, avec un commentaire dans lequel on retrouve son id.. Du coup avec cette lib, l'interaction avec le cron va être + simple :-)
via Nico
Pour rendre un json lisible :
cmd_qui_donne_du_json | python -mjson.tool
python ou perl, faudrait choisir pour concevoir des scripts d'admin moins crades qu'en bash
Objectif : tapper /obug dans xchat m'ouvre un onglet dans ff vers l'url du bug
1) activer le module python dans les options
2) créer un fichier .py dans /home/arnaud/.xchat2/
3)
module_name = "openbuginredmine"
module_version = "1.0"
module_description = "module to open bug in redmine"
import os
import xchat
def obug(word, word_eol, userdata):
if len(word) < 2:
print "Second arg must be the bug number!"
else:
#xchat.command("NOTICE @%s %s" % (xchat.get_info("channel"), word_eol[1]))
os.system("firefox https://monurlredmine.com/issues/"+word[1])
return xchat.EAT_ALL
xchat.hook_command("obug", obug, help="/obug open bug in ff")
Et voilà, plus qu'à reboot le xchat, ou bien /load nomdufichier.py
ça peut etre appliqué à plein de trucs : pouvoir lancer ce qu'on veut depuis xchat
Différence entre python et php
Et un site pour débuter en python lorsqu'on vient de php :
http://www.inspyration.org/tutoriels/debuter-python-lorsque-lon-vient-de-php
Qu'est ce que l'unicode ? qu'est-ce qu'un encodage ? Comment python gère ça
Autres liens dans l'article à lire :
http://www.cs.tut.fi/~jkorpela/unicode/guide.html
http://www.joelonsoftware.com/articles/Unicode.html
http://en.wikipedia.org/wiki/Character_encoding
http://en.wikipedia.org/wiki/UTF-8
Forum pour demander de l'aide/chercher des réponses sur Python
Site pour apprendre à développer avec une approche interactive. Python en particulier