#socket #socketserver

 

 

 1 #通过socket 实现简单的ssh#服务端
 2 
 3 #服务端
 4 import os
 5 import socket
 6 
 7 server = socket.socket()
 8 
 9 #server.bind(('0.0.0.0',9999))
10 server.bind(('localhost',9999))
11 server.alisten()
12 
13 while True:
14     conn,addr = server.accept()
15     print ('new conn:',addr)
16     while True:
17         print ('等待新指令')
18         data = conn.recv(1024)
19         if not data:
20             print ('客户端已经断开')
21             break
22 
23         print ('执行指令:',data)
24         cmd_res = os.poen(data.decode()).read()#接收字符串,执行结果也是字符串
25         print('before send',len(cmd_res))
26         if len(cmd_res) == 0 :
27             cmd_res = 'cmd has no output...'
28 
29         conn.send(str(len(cmd_res.encode())).encode('utf-8'))  #先发文件大小给客户端
30         conn.send(cmd_res.encode('utf-8')) 
31         print('send done')
32 
33 server.close()
#通过socket 实现简单的ssh#服务端 
 1 #通过socket 实现简单的ssh#客户端
 2 
 3 #客户端
 4 
 5 import socket
 6 
 7 client = socket.socket()
 8 
 9 #client.connect(('192.168.16.200',9999))
10 client.connect(('localhost',9999))
11 
12 while True:
13     cmd =input('>>>>:').strip()
14     if len(cmd) == 0:continue
15     client.send(cmd.encode('utf-8'))
16 
17     cmd_rer_size = client.recv(1024)#接收命令结果的长度(文件大小)
18     #cmd_res = client.recv(1024)
19     
20     print ('命令结果大小:',cmd_res._size)
21 
22     received_size = 0  
23 
24     received_data = b''
25     
26     while received_size < int(cmd_res_size.decode()):
27         data = client.recv(1024)
28         received_size += len(data) #每次收到的有可能小于1024,所以必须用len判断
29         #print (data.decode())
30         #print (received_size)
31         received_data += data
32         
33     else:
34         print (' cmd res receive done...',received_size)
35         print (received_data.decode())
36 
37         
38 client.close()
#通过socket 实现简单的ssh#客户端

 

 

  1 '''
  2 
  3 #通过socket接收大数据2#服务端
  4 
  5 #通过socket 实现简单的ssh#服务端
  6 
  7 #服务端A
  8 import time
  9 import os
 10 import socket
 11 
 12 server = socket.socket()
 13 
 14 server.bind(('0.0.0.0',9999))
 15 #server.bind(('localhost',9999))
 16 server.listen()
 17 
 18 while True:
 19     conn,addr = server.accept()
 20     print ('new conn:',addr)
 21     while True:
 22         print ('等待新指令')
 23         data = conn.recv(1024)
 24         if not data:
 25             print ('客户端已经断开')
 26             break
 27 
 28         print ('执行指令:',data)
 29         cmd_res = os.popen(data.decode()).read()#接收字符串,执行结果也是字符串
 30         print('before send',len(cmd_res))
 31         if len(cmd_res) == 0 :
 32             cmd_res = 'cmd has no output...'
 33 
 34         conn.send(str(len(cmd_res.encode())).encode('utf-8'))  #先发文件大小给客户端
 35         #time.sleep(0.5)   #socket 粘包,解决方法不好
 36 
 37         client_ack = conn.recv(1024) #等待客户端确认
 38         print ('ack from client:',client_ack)
 39 
 40         conn.send(cmd_res.encode('utf-8')) 
 41         print('send done')
 42 
 43 server.close()
 44 
 45 
 46 '''
 47 '''
 48 import hashlib
 49 m = hashlib.md5
 50 m.update(b'test')
 51 m.update(b'abc')
 52 
 53 
 54 
 55 '''
 56 
 57 
 58 '''
 59 
 60 #服务端B
 61 import hashlib
 62 import time
 63 import os
 64 import socket
 65 
 66 server = socket.socket()
 67 
 68 server.bind(('0.0.0.0',9999))
 69 #server.bind(('localhost',9999))
 70 server.listen()
 71 
 72 while True:
 73     conn,addr = server.accept()
 74     print ('new conn:',addr)
 75     while True:
 76         print ('等待新指令')
 77         data = conn.recv(1024)
 78         if not data:
 79             print ('客户端已经断开')
 80             break
 81 
 82         cmd,filename = data.decode().split()
 83         print (filename)
 84 
 85         if os.path.isfile(filename):
 86             f =open(filename,'rb')
 87             m=hashlib.md5()
 88             file_size =os.stat(filename).st_size
 89             conn.send(str(file_size).encode())#文件大小
 90             conn.recv(1024)#等待 ack
 91             for line in f:
 92                 #m.update(line)
 93                 conn.send(line)
 94             #print ('file md5',m.hexdigest())
 95             f.close()
 96         print('send done')
 97 
 98 server.close()
 99 
100 '''
101 
102 
103 #服务端C
104 import hashlib
105 import time
106 import os
107 import socket
108 
109 server = socket.socket()
110 
111 server.bind(('0.0.0.0',9999))
112 #server.bind(('localhost',9999))
113 server.listen()
114 
115 while True:
116     conn,addr = server.accept()
117     print ('new conn:',addr)
118     while True:
119         print ('等待新指令')
120         data = conn.recv(1024)
121         if not data:
122             print ('客户端已经断开')
123             break
124 
125         cmd,filename = data.decode().split()
126         print (filename)
127 
128         if os.path.isfile(filename):
129             f =open(filename,'rb')
130             m=hashlib.md5()
131             file_size =os.stat(filename).st_size
132             conn.send(str(file_size).encode())#文件大小
133             conn.recv(1024)#等待 ack
134             for line in f:
135                 m.update(line)
136                 conn.send(line)
137             print ('file md5',m.hexdigest())
138             f.close()
139             conn.send(m.hexdigest().encode())#mp5
140             
141         print('send done')
142 
143 server.close()
#通过socket接收大数据2#服务端
  1 '''
  2 #通过socket接收大数据2#客户端
  3 
  4 #通过socket 实现简单的ssh#客户端
  5 
  6 #客户端A
  7 
  8 import socket
  9 
 10 client = socket.socket()
 11 
 12 #client.connect(('192.168.80.20',9999))
 13 client.connect(('localhost',9999))
 14 
 15 while True:
 16     cmd =input('>>>>:').strip()
 17     if len(cmd) == 0:continue
 18     client.send(cmd.encode('utf-8'))
 19 
 20     cmd_rer_size = client.recv(1024)#接收命令结果的长度(文件大小)
 21     #cmd_res = client.recv(1024)
 22     
 23     print ('命令结果大小:',cmd_res._size)
 24     client.send('准备好接收了,loser可以发了'.encode('utf-8'))
 25     received_size = 0  
 26 
 27     received_data = b''
 28     
 29     while received_size < int(cmd_res_size.decode()):
 30         data = client.recv(1024)
 31         received_size += len(data) #每次收到的有可能小于1024,所以必须用len判断
 32         #print (data.decode())
 33         #print (received_size)
 34         received_data += data
 35         
 36     else:
 37         print (' cmd res receive done...',received_size)
 38         print (received_data.decode())
 39 
 40         
 41 client.close()
 42 
 43 '''
 44 
 45 
 46 '''
 47 #客户端B
 48 
 49 import socket
 50 
 51 client = socket.socket()
 52 
 53 #client.connect(('192.168.80.20',9999))
 54 client.connect(('localhost',9999))
 55 
 56 while True:
 57     cmd =input('>>>>:').strip()
 58     if len(cmd) == 0:continue
 59     if cmd.startswith('get'):
 60         client.send(cmd.encode())
 61         server_response = client.recv(1024)
 62         print ('servr response',server_response)
 63         client.send(b'ready to recv file')
 64         file_total_size = int (server_response.decode())
 65         received_size = 0
 66         filename = cmd.split()[1]
 67         f =open (filename +'.new','wb')
 68         while received_size < file_total_size:
 69             data =client.recv(1024)
 70             received_size += len(data)
 71             f.write(data)
 72             #print (file_total_size,received_size)
 73         else:
 74             print('file recv done',received_size,file_total_size)
 75             f.close()       
 76 client.close()
 77 
 78 
 79 '''
 80 
 81 #客户端C
 82 
 83 import socket
 84 import hashlib
 85 client = socket.socket()
 86 
 87 #client.connect(('192.168.80.20',9999))
 88 client.connect(('localhost',9999))
 89 
 90 while True:
 91     cmd =input('>>>>:').strip()
 92     if len(cmd) == 0:continue
 93     if cmd.startswith('get'):
 94         client.send(cmd.encode())
 95         server_response = client.recv(1024)
 96         print ('servr response',server_response)
 97         client.send(b'ready to recv file')
 98         file_total_size = int (server_response.decode())
 99         received_size = 0
100         filename = cmd.split()[1]
101         f = open (filename +'.new','wb')
102         m = hashlib.md5()
103         
104         while received_size < file_total_size:
105             if file_total_size - received_size > 1024 :#要收不止一次
106                 size =1024
107             else:  #最后一次了,剩下多少收多少
108                 size = file_total_size - received_size
109                 print ('lastreceive:',size)
110             data =client.recv(size)
111             received_size += len(data)
112             m.update(data)
113             f.write(data)
114             #print (file_total_size,received_size)
115         else:
116             new_file_md5 = m.hexdigest()
117             print('file recv done',received_size,file_total_size)
118             f.close()
119         server_file_md5 = client.recv(1024)
120         print ('server file md5:',server_file_md5)
121         print ('clientr file md5:',new_file_md5)
122         
123 client.close()
#通过socket接收大数据2#客户端

 

 

 

 1 #socketserver基本使用#服务器端
 2 '''
 3 1 你必须创建一个请求处理类。并且这个类要继承BaseRequestHandler,
 4         并且还要重写父亲类的handle()
 5 
 6 2 你必须实例化TCPServer,并且传递server ip 和你上面创建的请求处理类
 7         给这个TCPServer
 8 
 9 3 调用  Server.handle_request()#只处理一个请求
10         Server.serve_forever()#处理多个请求,并且永远执行
11         
12 4 关闭服务
13 '''
14 
15 
16 #socketserver基本使用
17 
18 
19 
20 import socketserver
21 
22 class MyTCPHandler(socketserver.BaseRequestHandler):
23     def handle(self):
24         while True:
25             try:
26                 self.data =self.request.recv(1024).strip()
27                 print ('{} wrote:'.format(self.client_address[0]))
28                 print (self.data)
29                 self.request.send(self.data.upper())
30             except ConnectionResetError as e:
31                 print ('err,e')
32                 break
33 
34 if __name__ == '__main__':
35     HOST,PORT = 'localhost',9999
36     server = socketserver.TCPServer((HOST,PORT),MyTCPHandler)
37     server.serve_forever()
#socketserver基本使用#服务器端
 1 #socketserver基本使用#客户端
 2 '''
 3 1 你必须创建一个请求处理类。并且这个类要继承BaseRequestHandler,
 4         并且还要重写父亲类的handle()
 5 
 6 2 你必须实例化TCPServer,并且传递server ip 和你上面创建的请求处理类
 7         给这个TCPServer
 8 
 9 3 调用  Server.handle_request()#只处理一个请求
10         Server.serve_forever()#处理多个请求,并且永远执行
11         
12 4 关闭服务
13 '''
14 
15 
16 #socketserver基本使用#客户端
17 
18 
19 
20 import socket
21 
22 client = socket.socket()#声明socket类型,同时生成socket连接对象
23 
24 client.connect(('localhost',9999))
25 
26 while True:
27     msg =input('>>:').strip()
28     if len(msg) == 0:continue
29     client.send(msg.encode('utf-8'))
30     data = client.recv(10240)
31     print ('recv:',data.decode())
32 
33 client.close()
#socketserver基本使用#客户端

 

 

 1 #socketserver 多线程(多并发)使用#服务端
 2 '''
 3 1 你必须创建一个请求处理类。并且这个类要继承BaseRequestHandler,
 4         并且还要重写父亲类的handle()
 5 
 6 2 你必须实例化TCPServer,并且传递server ip 和你上面创建的请求处理类
 7         给这个TCPServer
 8 
 9 3 调用  Server.handle_request()#只处理一个请求
10         Server.serve_forever()#处理多个请求,并且永远执行
11         
12 4 关闭服务
13 '''
14 #普通socket地址重用
15 #server = socket.socket() #获得socket实例
16 #server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
17 
18 #socketserver 地址重用
19 #self.allow_reuse_address
20 
21 import socketserver
22 
23 class MyTCPHandler(socketserver.BaseRequestHandler):
24     def handle(self):
25         while True:
26             try:
27                 self.data =self.request.recv(1024).strip()
28                 print ('{} wrote:'.format(self.client_address[0]))
29                 print (self.data)
30                 self.request.send(self.data.upper())
31             except ConnectionResetError as e:
32                 print ('err,e')
33                 break
34 
35 if __name__ == '__main__':
36     HOST,PORT = 'localhost',9999
37     
38     server = socketserver.ThreadingTCPServer((HOST,PORT),MyTCPHandler)
39     server.serve_forever()
#socketserver 多线程(多并发)使用#服务端

 

 

 1 #socketserver 多进程使用#服务端 WIN不能使用
 2 '''
 3 1 你必须创建一个请求处理类。并且这个类要继承BaseRequestHandler,
 4         并且还要重写父亲类的handle()
 5 
 6 2 你必须实例化TCPServer,并且传递server ip 和你上面创建的请求处理类
 7         给这个TCPServer
 8 
 9 3 调用  Server.handle_request()#只处理一个请求
10         Server.serve_forever()#处理多个请求,并且永远执行
11         
12 4 关闭服务
13 '''
14 
15 import socketserver
16 
17 class MyTCPHandler(socketserver.BaseRequestHandler):
18     def handle(self):
19         while True:
20             try:
21                 self.data =self.request.recv(1024).strip()
22                 print ('{} wrote:'.format(self.client_address[0]))
23                 print (self.data)
24                 self.request.send(self.data.upper())
25             except ConnectionResetError as e:
26                 print ('err,e')
27                 break
28 
29 if __name__ == '__main__':
30     HOST,PORT = 'localhost',9999
31     server = socketserver.ForkingTCPServer((HOST,PORT),MyTCPHandler)
32     server.serve_forever()
#socketserver 多进程使用#服务端 WIN不能使用

 

posted @ 2017-08-10 15:45  颜言  阅读(126)  评论(0编辑  收藏  举报