Skip to content

Instantly share code, notes, and snippets.

View selfboot's full-sized avatar
🎯
Focusing

selfboot selfboot

🎯
Focusing
View GitHub Profile
@selfboot
selfboot / main.py
Created May 31, 2016 14:26 — forked from gear11/main.py
Simple Python proxy server based on Flask and Requests. See: http:/python-proxy-server/gear11.com/2013/12/python-proxy-server/
"""
A simple proxy server. Usage:
http://hostname:port/p/(URL to be proxied, minus protocol)
For example:
http://localhost:8080/p/www.google.com
"""
@selfboot
selfboot / timer.py
Created June 12, 2016 15:47
Simple Timer context manager. It will take care of starting the timer when your code block begins execution and stopping the timer when your code block ends.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
# @Last Modified time: 2016-06-12 23:46:06
from time import clock
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
@selfboot
selfboot / time_profile.py
Created June 13, 2016 12:07
Different ways to check the time cost!
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Last Modified time: 2016-06-13 20:00:34
orignal_str = "Profiling a Python program is doing a dynamic analysis"\
"that measures the execution time of the program and"\
"everything that compose it."
exec_times = 100000
# @profile
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
# @Last Modified time: 2016-06-13 20:54:09
import cProfile
from time_profile import *
import pstats
cProfile.run("timeit_profile()", "timeit")
p = pstats.Stats('timeit')
@selfboot
selfboot / flask_list_routes
Created June 15, 2016 12:34
list the routes
@manager.command
def list_routes():
"""List all the rules of route."""
import urllib
output = []
for rule in app.url_map.iter_rules():
options = {}
for arg in rule.arguments:
options[arg] = "[{0}]".format(arg)
@selfboot
selfboot / centos_python.sh
Last active June 8, 2023 14:11
CentOS 6.8: Install Python 2.7.10, pip, virtualenv, and virtualenvwrapper on CentOS
#!/bin/bash
# According to:
# How To Set Up Python 2.7.6 and 3.3.3 on CentOS 6.4
# https://www.digitalocean.com/community/tutorials/how-to-set-up-python-2-7-6-and-3-3-3-on-centos-6-4
yum -y update
yum groupinstall -y 'development tools'
yum install -y zlib-dev openssl-devel sqlite-devel bzip2-devel
yum install xz-libs
wget http://www.python.org/ftp/python/2.7.10/Python-2.7.10.tar.xz
@selfboot
selfboot / getsites.py
Created June 27, 2016 09:06
爬虫:百度搜索 关键字,返回指定页面范围内所有条目的题目和网站链接
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Last Modified time: 2016-06-27 17:03:01
import requests
from lxml import html as HTML
import os
import codecs
class KeyWordSite(object):
@selfboot
selfboot / mobileadapt.py
Created June 27, 2016 09:14
Check whether the site is mobile friendly using the query interface of bing.com. Can check many sites at the same time asynchronously with gevent.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Last Modified time: 2016-06-27 17:03:25
from gevent import monkey
monkey.patch_all()
import time
import requests
from lxml import html as HTML
import gevent
from os.path import isfile
@selfboot
selfboot / speedtest.py
Created June 27, 2016 09:15
Simple website speed test tools.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Last Modified time: 2016-06-27 17:03:42
import requests
import os
def get_speed_test(src_path, result_path, timeout=10):
all_sites = _get_sites_(src_path)
results = []
@selfboot
selfboot / multi_thread.py
Created June 28, 2016 02:51
对于IO密集型任务来说,多线程仍然有一定的效果。因为在I/O请求的时候,当一个线程因I/O请求阻塞,可以调用另一个线程来继续发起对另一个Url的请求。当第一个线程的I/O请求得到回应时,它再次得到运行。
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Last Modified time: 2016-06-28 10:50:58
import urllib2
import time
from threading import Thread
# 在大量访问url的时候,多线程很有优势,实际上也是空间换时间
URLs = ["http://www.baidu.com",