Skip to content

Instantly share code, notes, and snippets.

@TonyPythoneer
TonyPythoneer / access_https.py
Created June 30, 2015 08:07
Use httplib2 to access https
#
import httplib2
from json import loads, dumps
header = {'Content-Type': 'application/json; charset=UTF-8'}
pem_path = "/path/to/domainname.pem"
h = httplib2.Http(ca_certs=pem_path, disable_ssl_certificate_validation=True)
req_dict = {
"url": "url",
"method": "method",
"body": dumps({"key": "value"}),
@TonyPythoneer
TonyPythoneer / list_urls_for_django.py
Created July 1, 2015 02:06
It's convenient to list urls for django
import urls
def show_urls(urllist, depth=0):
for entry in urllist:
print " " * depth, entry.regex.pattern
if hasattr(entry, 'url_patterns'):
show_urls(entry.url_patterns, depth + 1)
show_urls(urls.urlpatterns)
import timeit
a={u'is_public': [u'True']}
b={'name': u'', 'devie_type': u'', 'is_public': True, 'serial_num': u'', 'is_online': False, 'operation_system': u''}
def test1():
map(lambda (k,v): (k, b[k]), a.items())
def test2():
{k: b[k]
from threading import Thread
def example(a,b):
for i in range(1000):
pass
return a+b
thr = Thread(target=example, args=[1, 2])
print thr.start()
1.server安裝 Postfix (for smtp)
# apt-get install mailutils postfix
server configuration type : Internet Site
# vim /etc/postfix/main.cf
inet_interfaces = localhost
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<a target="_self" href="https://s1.yimg.com/rz/d/yahoo_frontpage_zh-Hant-TW_s_f_p_bestfit_frontpage_2x.png" download="picture">picture</a>
</body>
</html>
import string
s = string.ascii_uppercase
def convert_string_to_index(word):
word = word.upper()
s_expression = '+'.join([ letter for letter in word])
n_expression = '+'.join([ str(s.index(letter)+1) for letter in word])
total = sum([s.index(letter)+1 for letter in word])
print s_expression
class SignUpSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'email', 'password', 'first_name', 'last_name')
def save(self, *args, **kwargs):
"""create user
"""
User.objects.create_user(**self.validated_data)
'''
import Queue
import threading
import urllib2
# called by each thread
def get_url(q, url):
q.put(urllib2.urlopen(url).read())
theurls = ["http://google.com", "http://yahoo.com"]
upstream $repo_name {
server unix://$service_deployment_filename/websock.sock;
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name localhost;