多线程实例

1>>ping 一个网段的主机

 1 import subprocess
 2 import threading
 3 
 4 
 5 class Ping:
 6     def __init__(self, host):
 7         self.host = host
 8 
 9     def __call__(self):
10 
11         result = subprocess.run('ping -c2 %s &> /dev/null' % self.host, shell=True)
12         if result.returncode == 0:
13             print('%s:up' % self.host)
14         else:
15             print('%s:down' % self.host)
16 
17 
18 
19 if __name__ == "__main__":
20     ips = ['176.130.1.%s' % i for i in range(1,255)]
21     for ip in ips:
22         t = threading.Thread(target=Ping(ip))
23         t.start()

2.多进程实现ping

 1 import os
 2 import subprocess
 3 
 4 def ping(host):
 5     result = subprocess.run('ping -c2 %s &> /dev/null' %host,shell=True)
 6     if result.returncode == 0:
 7         print('%s:up' % host)
 8     else:
 9         print('%s:down' % host)
10 
11 def main():
12     ips = ['176.130.1.%s' % i for i in range(1,255)]
13     for ip in ips:
14         pid = os.fork()
15         if not pid:
16             ping(ip)
17             exit()
18 
19 
20 
21 if __name__ == "__main__":
22     main()

 


3.多进程实现socket通信 

 1 import socket
 2 from time import strftime
 3 import os
 4 
 5 
 6 class TcpTimeServer(object):
 7     def __init__(self, host='', port=12345):
 8         self.addr = (host, port)
 9         self.serv = socket.socket()
10 
11     def chat(self,cli_sock):
12         while True:
13             data = cli_sock.recv(1024)
14             if data.strip() == b'quit':
15                 break
16             data = data.decode()
17             data = '[%s] %s' %(strftime('%H:%M:%S'),data)
18             cli_sock.send(data.encode())
19         cli_sock.close()
20 
21     def run(self):
22         self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
23         self.serv.bind(self.addr)
24         self.serv.listen(1)
25 
26         while True:
27             try:
28                 cli_sock , cli_addr = self.serv.accept()
29             except KeyboardInterrupt:
30                 break
31             pid = os.fork()
32             if not pid:
33                 self.serv.close()   # 子进程不需要服务器套接字,关掉
34                 self.chat(cli_sock)
35                 exit()
36             cli_sock.close()        # 父进程不与客户及通信,关闭客户机套接字
37 
38             while True:
39                 result = os.waitpid(-1,1)[0]
40                 if result == 0:     # 不是僵尸进程,则终端循环
41                     break
42 
43         self.serv.close()
44 
45 
46 if __name__ == "__main__":
47     tcpserv = TcpTimeServer()
48     tcpserv.run()

 

4.多线程实现socket通讯

 1 import socket
 2 from time import strftime
 3 import threading
 4 
 5 class TcpTimeServer(object):
 6     def __init__(self, host='', port=12345):
 7         self.addr = (host, port)
 8         self.serv = socket.socket()
 9 
10     def chat(self,cli_sock):
11         while True:
12             data = cli_sock.recv(1024)
13             if data.strip() == b'quit':
14                 break
15             data = data.decode()
16             data = '[%s] %s' %(strftime('%H:%M:%S'),data)
17             cli_sock.send(data.encode())
18         cli_sock.close()
19 
20     def run(self):
21         self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
22         self.serv.bind(self.addr)
23         self.serv.listen(1)
24 
25         while True:
26             try:
27                 cli_sock , cli_addr = self.serv.accept()
28             except KeyboardInterrupt:
29                 break
30             t = threading.Thread(target=self.chat, args=(cli_sock,))
31             t.start()
32 
33         self.serv.close()
34 
35 
36 if __name__ == "__main__":
37     tcpserv = TcpTimeServer()
38     tcpserv.run()

 

posted @ 2019-03-22 16:48  Ray_chen  阅读(271)  评论(1编辑  收藏  举报