#作业: Code that transmits large files, nothing to say, #server. PY Import OS Import JSON import socket import struct filepath = R ' D:\1.py ' SK = Socket.socke T () Sk.bind ((' 127.0.0.1 ', 9000)) Sk.listen () conn,addr = sk.accept () filename = os.path.basename (filepath) filesize = OS.P Ath.getsize (filepath) dic = {' filename ': filename, ' filesize ': filesize} str_dic = Json.dumps (DIC). Encode (' Utf-8 ') len_ DIC = Len (str_dic) length = Struct.pack (' i ', Len_dic) conn.send (lengths) # dic (conn.send) # dic with open (file Path, ' RB ') as F: # file while filesize:content = F.read (4096) conn.send (content) FileSize = l En (content) conn.close () Sk.close () #client. PY Import JSON import struct import Socket SK = Socket.socket () sk.connect (
(' 127.0.0.1 ', 9000)) Dic_len = SK.RECV (4) Dic_len = Struct.unpack (' i ', Dic_len) [0] dic = SK.RECV (dic_len) str_dic = Dic.decode (' utf-8 ') dic = JS
On.loads (Str_dic) with open (dic[' filename '], ' WB ') as F:while dic[' filesize ']: content = SK.RECV (4096) Dic[' filEsize ']-= len (content) f.write (content) Sk.close ()
#验证客户端合法的代码, #server. py Import OS import socket import HMAC Secret_key = ' Rite shampoo with rejoice '. Encode (' Utf-8 ') #服务器端与客户端,
Has the same key, SK = Socket.socket () sk.bind ((' 127.0.0.1 ', 9000)) Sk.listen () while true:try:conn,addr = Sk.accept ()
Random_bytes = Os.urandom #产生一个bytes类型的随机数, and then sent to the client, Conn.send (random_bytes) obj = hmac.new (key =secret_key,msg =random_bytes) #key是密钥, MSG is the message to be encrypted, ret = Obj.hexdigest () #得到加密的结果, msg = CONN.RECV (1024). Decode (' Utf-8 ') #得到客户端的加密结果 if msg = RET :p rint (' legitimate client ') #如果相同, stating that the client and server-side keys are the same, Else:conn.close () finally:sk.close () b Reak #client. PY Import Socket import HMAC Secret_key = ' Rite shampoo with rejoice '. Encode (' Utf-8 ') SK = Socket.socket (' 127.0 .0.1 ', 9000)) Urandom = SK.RECV (1024) #得到服务器的随机数, hmac_obj = hmac.new (key = Secret_ke Y,msg =urandom)#进行加密, Sk.send (Hmac_obj.hexdigest (). Encode (' Utf-8 ')) #把加密结果发送给服务器端, if the same is approved, Sk.close ()
#并发编程
#server. py
Import Socketserver
class MyServer (Socketserver. Baserequesthandler):
def handle (self):
self.request.send (b ' Hello ')
msg = SELF.REQUEST.RECV (1024)
Print (msg)
print (self.server) print (
self.request) print (
self.client_address)
if __name__ = = ' __ Main__ ':
socketserver. tcpserver.allow_reuse_address = True
Server = Socketserver. Threadingtcpserver (' 127.0.0.1 ', 9000), MyServer)
server.serve_forever ()
#client. py
#因为是并发, So the following client program can run multiple.
Import Socket
sk = Socket.socket ()
sk.connect (' 127.0.0.1 ', 9000))
msg = SK.RECV (1024)
Print (msg)
sk.send (b ' bye ')
sk.close ()
#下面博客对socketserver写的蛮好,
#https://www.cnblogs.com/ tianjianng/p/7712648.html
#https://www.cnblogs.com/progor/p/8617042.html
#https://blog.csdn.net/ yannanxiu/article/details/51735452
Import HMAC
obj = hmac.new (key = B ' Secret_key ', msg =b ' 11010012922 ')
print (Obj.hexdigest ())
obj = hmac.new ( Key = B ' Secret_key ', msg =b ' 11010012922 ')
print (Obj.hexdigest ())