Skip to content

Instantly share code, notes, and snippets.

View guoqiao's full-sized avatar

Guo Qiao (Joe) guoqiao

  • Canonical
  • Auckland, New Zealand
  • X @guoqiao
View GitHub Profile
@guoqiao
guoqiao / Django logging config example
Created September 12, 2016 23:32
Add DB Handler for Python logging
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(levelname)s %(asctime)s %(name)s: %(message)s'
},
},
'handlers': {
'console': {
@guoqiao
guoqiao / xml_extract.py
Created September 14, 2016 21:36
Extract element with namescape from xml by Python xml module
from xml.etree import ElementTree as ET
for prefix, uri in NAMESPACES.items():
ET.register_namespace(prefix, uri)
root_xml_element = ET.fromstring(xml)
token_xml_element = root_xml_element.find(
'soap:Body', namespaces=NAMESPACES
).find(
'wst:RequestSecurityTokenResponseCollection', namespaces=NAMESPACES
).find(
'wst:RequestSecurityTokenResponse', namespaces=NAMESPACES
@guoqiao
guoqiao / saml.html
Created September 16, 2016 00:16
how to ask browser to post data automatically after redirect
HTTP/1.1 200 OK
Date: 21 Jan 2004 07:00:49 GMT
Content-Type: text/html; charset=iso-8859-1
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<body onload="document.forms[0].submit()">
<noscript>
<p>
@guoqiao
guoqiao / register.py
Created September 20, 2016 02:52
example for register decorator
_functions = {}
def register(f):
global _functions
_functions[f.__name__] = f
return f
@register
def foo():
return 'bar'
@guoqiao
guoqiao / temporarily.py
Created August 18, 2017 03:11
Use contextmanager to set attrs for object temporarily
from contextlib import contextmanager
@contextmanager
def temporarily(obj, **kwargs):
original_values = {k: getattr(obj, k) for k in kwargs}
for k, v in kwargs.items():
setattr(obj, k, v)
obj.save(update_fields=kwargs.keys())
try:
yield
@guoqiao
guoqiao / baidu1.js
Last active August 19, 2017 11:32
Baidu tongji tries to inject data to my site to attack Google
eval(function(p, a, c, k, e, d) {
e = function(c) {
return (c < a ? "" : e(parseInt(c / a))) + ((c = c % a) > 35 ? String.fromCharCode(c + 29) : c.toString(36))
}
;
if (!''.replace(/^/, String)) {
while (c--)
d[e(c)] = k[c] || e(c);
k = [function(e) {
return d[e]
@guoqiao
guoqiao / validate_email.py
Created August 21, 2017 21:21
validate email with django builtin functions
def validate_email(email):
from django.core.validators import EmailValidator
from django.core.exceptions import ValidateError
try:
EmailValidator()(email)
except ValidateError:
return False
return True
@guoqiao
guoqiao / reboot.yml
Last active July 23, 2018 22:32
ansible tasks to reboot server and wait it come back
- name: restart server if ulimit changed
become: yes
shell: sleep 2 && reboot
async: 1
poll: 0
ignore_errors: yes
- name: wait for server to come back
local_action:
module: wait_for
@guoqiao
guoqiao / smtp.py
Last active December 6, 2018 21:24
Send email with SMTP over STARTTLS in Python
#!/usr/bin/env python
# coding=utf8
"""
Send email with SMTP
Add env vars in your ~/.bashrc and source it:
export EMAIL_HOST=smtp.example.com
export EMAIL_PORT=587
export EMAIL_USERNAME=sender@example.com
@guoqiao
guoqiao / Vagrantfile
Last active January 22, 2019 03:41
Vagrantfile example with multiple machines.
# -*- mode: ruby -*-
Vagrant.configure("2") do |config|
config.ssh.insert_key = false
config.vm.provider "virtualbox" do |v|
v.memory = 4096
v.cpus = 4
end
config.vm.define "dc0" do |dc|
dc.vm.box = "samba-ubuntu1604"