python代码收集mark

## 1 flask 中jinjia2的日期格式化
* 添加filter到app的环境变量中
“`python
#unix日期格式化。并可提供到jinjia模板中
def dateformat(value, format=”%Y-%m-%d %H:%M:%S”):
value=int(value)
value = time.localtime(value)
return time.strftime(format,value)
app = Flask(__name__)
app.debug = True
app.jinja_env.filters[‘dateformat’] = dateformat
………..
@app.route(‘/test’)
def test():
abc=’1447405576′
return render_template(‘t.html’,info=abc)
#in template
{% include “head.html” %}

  • {{info|dateformat(“%Y-%m-%d”)}}

{% include “footer.html” %}
“`

#代理校验 proxy

import json
from requests import *
import time

#vproxy 来自https://github.com/fate0/getproxy
proxyresult=[]
def checka():
    for proxy in open("vproxy.txt"):
        proxyobj = json.loads(proxy)
        _validate_proxy(proxyobj)
    print "the end"

def _validate_proxy(proxy):
    country = proxy.get('country')
    host = proxy.get('host')
    port = proxy.get('port')
    scheme=proxy.get('type')

    proxy_hash = '%s://%s:%s' % (scheme, host, port)
    request_proxies = {
        scheme: "%s:%s" % (host, port)
    }
    request_begin = time.time()
    #"%s://httpbin.org/get?show_env=1&cur=%s" % (scheme, request_begin),
    try:
        response_json = get(
            "%s://www.so-cools.com/get?show_env=1&cur=%s" % (scheme, request_begin),
            proxies=request_proxies,
            timeout=5
        ).json()
    except:
        return

    request_end = time.time()

    if str(request_begin) != response_json.get('args', {}).get('cur', ''):
        return
    print {'host':host,'port':port,'scheme':scheme,'response_time':request_end-request_begin}
    proxyresult.append({'host':host,'port':port,'scheme':scheme,'response_time':request_end-request_begin})


checka()
proxyresult.sort(key = lambda x:x["response_time"])
print proxyresult

#快速指数算法

def quick_mod(a,p,n):   
    result = a % n  
    remainders = []  
    while p != 1:  
        remainders.append(p & 1)  
        p = p >> 1  
    print(remainders)  
    while remainders:  
        rem = remainders.pop()  
        result = ((a ** rem) * result ** 2) % n  
    return result  


#xx=quick_mod(225,29,323)       
#xx=quick_mod(2,56,1001)
#xx=quick_mod(62,65,133)
#xx=quick_mod(2,1000,89)
#xx=quick_mod(3,201,11)
#xx=quick_mod(4,201,11)
xx=quick_mod(2,1000,89)
print xx

发表评论

电子邮件地址不会被公开。 必填项已用*标注