国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python利用socketserver實現(xiàn)并發(fā)套接字功能

python利用socketserver實現(xiàn)并發(fā)套接字功能

2021-01-09 00:28成長的網(wǎng)工 Python

這篇文章主要為大家詳細(xì)介紹了python利用socketserver實現(xiàn)并發(fā)套接字功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實現(xiàn)利用pythonsocketserver這個強(qiáng)大的模塊實現(xiàn)套接字的并發(fā),具體內(nèi)容如下

目錄結(jié)構(gòu)如下:

python利用socketserver實現(xiàn)并發(fā)套接字功能

測試文件請放在server_file文件夾里面

server.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: gbk -*-
# @Version : Python 3.5.2
# @Time : 2018/1/24 10:29
# @Author : Ncp
# @File : server.py
# @Software: PyCharm
 
import json
import time
import hashlib
import struct
import os
from socketserver import *
 
FILE_PATH = os.path.dirname(os.path.abspath(__file__))+'\\server_file'
 
class MYserver(BaseRequestHandler): # 設(shè)置一個類,基礎(chǔ)BaseRequestHandler這個類
 def handle(self):     # 這個方法下添加通信功能(和上面創(chuàng)建類一樣,這是socketserver的固定模式)
  print(self.client_address)
  '''
  :functions: 使用socketserver的并發(fā)套接字,提供客戶端下載文件,并對文件進(jìn)行MD5加密
  '''
  while True:
   try:
    data = self.request.recv(1024)
    data_recv = data.decode('gbk').split()
    if not os.path.exists(FILE_PATH+r'\%s' %data_recv[1]):
     self.request.send('file is not found'.encode('gbk'))
     continue
    else:
     data = self.request.send('1'.encode('gbk')) # 這里發(fā)現(xiàn)小問題,不回復(fù)一個信息的話,發(fā)送給客戶端的包頭居然成了沒有封裝
     FILE_SIZE = os.path.getsize(FILE_PATH+r'\%s' %data_recv[1])
     with open(FILE_PATH+r'\%s' %data_recv[1],'rb')as f:
      hash_file = f.read()
     m = hashlib.md5()
     m.update(hash_file)
     m_hex = m.hexdigest()
     file_header = {'filename':data_recv[1],
         'filesize':FILE_SIZE,
         'md5':m_hex,
         'time':time.strftime('%Y-%m-%d-%X',time.localtime())
         }
     # 包頭信息序列化
     file_header_dump = json.dumps(file_header)
     # 編譯成2進(jìn)制
     file_header_bytes = file_header_dump.encode('gbk')
     # 封裝報頭
     file_header_struct = struct.pack('i',len(file_header_bytes))
     # 發(fā)送報頭
     self.request.send(file_header_struct)
     # 發(fā)送報文內(nèi)容
     self.request.send(file_header_bytes)
     # 發(fā)送文件數(shù)據(jù)
     send_size = 0
     with open(FILE_PATH+r'\%s' %data_recv[1] , 'rb')as f:
      for i in f:
       self.request.send(i)
       send_size += len(i) # 這里后續(xù)可以拓展一個進(jìn)度或者網(wǎng)速顯示功能
   except Exception:
    self.request.close()
 
 
if __name__ == '__main__':
 server = ThreadingTCPServer(('127.0.0.1',8080),MYserver) # windows下只能開啟多線程
 server.serve_forever()

client.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: gbk -*-
# @Version : Python 3.5.2
# @Time : 2018/1/24 10:29
# @Author : Ncp
# @File : client.py
# @Software: PyCharm
 
from socket import *
import os,sys
import hashlib
import struct
import math
import json
 
FILE_PATH = os.path.dirname(os.path.abspath(__file__))+'\\client_file'
 
 
# 顯示下載進(jìn)度條功能,可以拓展為顯示下載速度(提示,因為每次傳輸4096個字節(jié),那么下載網(wǎng)速為KB/S,1KB個字節(jié)=1024B(字節(jié)),那么1s傳輸了多少個字節(jié)呢?)
def progress(recvd, total):
 fraction = '{:.0%}'.format(recvd / total)
 sys.stdout.write('\r[%-30s] %s' % ('#' * int(math.floor(recvd * 30 / total)), fraction))
 sys.stdout.flush()
 if recvd == total:
  sys.stdout.write('\n')
 
 
# 主函數(shù)
def run(ip,addr):
 client = socket(AF_INET,SOCK_STREAM)
 client.connect((ip,addr))
 while True:
  user_input = input('>>').strip()
  cmd = user_input.split()
  if len(cmd) != 2:
   print('input format is error please use:get xx')
   continue
  if cmd[0] == 'get':
   client.send(user_input.encode('gbk'))
   data = client.recv(1024)
   data_recv = data.decode('gbk')
   if data_recv == 'file is not found':
    print(data_recv)
    continue
  else:
   print('commands is not found')
   continue
  # 收包頭,然后一系列處理
  header = client.recv(4)
  if not header:break
  header_json_len = struct.unpack('i', header)[0]
  header_json_bytes = client.recv(header_json_len)
  header_josn = header_json_bytes.decode('gbk')
  header_dic = json.loads(header_josn)
  # 去除包頭內(nèi)容進(jìn)行下載
  print(header_dic)
  data_len = header_dic['filesize']
  data_file = header_dic['filename']
  data_md5 = header_dic['md5']
  recv_size = 0
  with open(FILE_PATH+r'\%s' %data_file,'wb')as fw:
   while recv_size < data_len:
    recv_data = client.recv(4096)
    recv_size += len(recv_data)
    fw.write(recv_data)
    progress(recv_size,data_len)
   print('Download completion, start validation')
  # 收到文件后,讀取文件進(jìn)行加密,看是否與服務(wù)端下載的文件一致!
  with open(FILE_PATH+r'\%s' %data_file,'rb')as fr:
   data_total = fr.read()
 
  m = hashlib.md5()
  m.update(data_total)
  m_hex = m.hexdigest()
 
  if m_hex == data_md5:
   print('文件驗證通過')
  else:
   print('文件損壞,刪除文件')
   os.remove(FILE_PATH+r'\%s' %data_file)
 
 
if __name__ == '__main__':
 run('127.0.0.1',8080)

自己可以設(shè)置一個多用戶登錄,然后測試,用戶下載同一個文件,分別存入每個用戶自己的家目錄下面,效果更好。

當(dāng)然現(xiàn)在也能測試,開啟兩個客戶端同時下載文件。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.cnblogs.com/encp/archive/2018/01/26/8358432.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 污视频网站在线观看 | 激情综合激情 | a级黄色在线观看 | 伊人婷婷 | 狠狠躁躁夜夜躁波多野结依 | 色.com| av黄网站 | 亚洲成人av片 | 久久久久av69精品 | 久久久久综合 | 午夜网址 | 久久久精品视频网站 | 99国产精品 | 日韩中文在线 | 一级在线| 久久精品国产v日韩v亚洲 | 成视频年人免费看黄网站 | 在线不卡视频 | 久久在线视频 | 中文字幕亚洲欧美日韩在线不卡 | 亚洲+变态+欧美+另类+精品 | 国产欧美精品一区二区三区四区 | 日本久久久久久 | 国产精品3区 | 精品视频久久 | 欧美日韩在线免费观看 | 色av成人 | 中文字幕一二三 | 亚洲一区二区国产 | 国产韩国精品一区二区三区 | 亚洲男人天堂网 | 精品久久久久久久久久久久久久久久久久久 | 日韩午夜电影 | 一区二区三区无码高清视频 | 日韩在线观看中文字幕 | 亚洲精品成人悠悠色影视 | 人人爽人人爽人人片av | 国产资源在线视频 | 日韩久久久久久 | 久久不卡| 激情久久av一区av二区av三区 |