A. Stick package
Only tcp protocol stick package will occur, udp tcp agreement will not:
There is a co-sourcing mechanism (Nagle algorithm), and the transmission interval of many successive small data, a data transfer to be packaged
there is a mechanism unpacking mechanism, because by the data card MTU restrictions, split, split into several smaller data transmission
when the transmission to the target host operating system layer, re-combine multiple smaller data into the original data
Why stick udp packet does not happen?
udp stick package does not occur, a limit udp protocol and receive data according to the size of layer: 65535 - the IP header (20) - udp header (8) = 65507 stand data link layer, should be in general restricted to the card MTU 1500, so that the data link layer, the size of a retransmission data is limited to 1500 - ip header (20) - udp header (8) = 1472 conclusion: If the sendto (NUM) NUM > 65507 given 1472 < num <65507 will unpack the data link layer, and udp itself is unreliable protocol, once unpacked, the plurality of small data packets caused by transmission in the network, if any lost, then the data transmission fails num <1472 is an ideal state
II. Run (subprocess)
subprocess.Popen = cmd (cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE) # cmd: represents the system command # shell = True: This command is representative of system commands; tells the operating system, as the system cmd command # stdout: after performing a system command, save the results of a pipeline for # stderr: after performing the system command is used to save the error of a pipeline
Service-Terminal:
import socket import subprocess sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() conn,addr = sk.accept() while 1: msg_r = conn.recv(1024).decode('utf-8') result = subprocess.Popen(msg_r,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) # print(result.stdout.read().decode('gbk')) # print(result.stderr.read()) stdout = result.stdout.read() stderr = result.stderr.read() if stderr: conn.send(stderr) else: conn.send(stdout) conn.close() sk.close()
Client:
'' ' Client sends a command to execute server executes the command, the result of execution returns to the client the client to get the results are presented in front of the user ' '' Import Socket SK = socket.socket () sk.connect_ex (( ' 127.0. 0.1 ' , 8080 )) the while . 1 : cmd = iNPUT ( ' enter system commands >>> ' ) sk.send (cmd.encode ( ' UTF-. 8 ' )) msg_r = sk.recv (10240) .decode ( ' GBK ' ) Print (msg_r) sk.close ()
III. Large file transfers
Service-Terminal:
import socket import json import struct sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() conn,addr = sk.accept() # str_dic = conn.recv(100).decode('utf-8') # dic = json.loads(str_dic) # print(dic) b_len_dic = conn.recv(4) len_dic = struct.unpack('i',b_len_dic)[0] str_dic = conn.recv(len_dic).decode('utf-8') dic = json.loads(str_dic) print(dic) # conn.send(b'OK') if dic['opt'] == 'upload': filename = '1' + dic['filename'] with open(filename,mode='ab') as f: while dic['filesize']: content = conn.recv(1024) f.write(content) dic['filesize'] -= len(content) conn.close() sk.close()
Client:
Import Socket Import OS Import JSON Import struct SK = socket.socket () sk.connect_ex (( ' 127.0.0.1 ' , 8080 )) meau = { ' . 1 ' : ' Upload ' , ' 2 ' : ' downloads ' } NUM = iNPUT ( ' enter optional function >>> ' ) DIC = { ' opt ' : meau.get (NUM), 'filename': None, ' filesize ' : None} file_path = INPUT ( ' Enter an absolute path >>> ' ) filename = os.path.basename (file_path) DIC [ ' filename ' ] = filename filesize = os.path.getsize ( file_path) DIC [ ' filesize ' ] = filesize str_dic = json.dumps (DIC) len_dic = len (str_dic) b_len_dic = struct.pack ( ' I ' , len_dic) # length packed into the dictionary data of a 4bytes sk.send (b_len_dic + str_dic.encode ( ' UTF-8 ' )) # sk.recv (1024) # Why here plus a recv? It is to prevent a too fast send program execution, send the following program results in the occurrence of stick package thereto, so that data confusion occurs with Open (file_path, MODE = ' RB ' ) AS F: the while filesize: Content = reached, f.read (1024 ) sk.send (Content) filesize - = len (Content) sk.close ()
IV. Change directory
Service-Terminal:
import socket import os sk = socket.socket() sk.bind(('127.0.0.1', 8080)) sk.listen() conn, addr = sk.accept() def send_data(conn, path): lis_dir = os.listdir(path) str_dir = '__'.join(lis_dir) conn.send(str_dir.encode('utf-8')) abs_path = conn.recv(1024).decode('utf-8') current_dir = abs_path send_data(conn, current_dir) while 1: cmd = conn.recv(1024).decode('utf-8') if cmd == '..': current_dir = '/'.join(current_dir.split('\\')[:-1]) print(current_dir) send_data(conn,current_dir) else: filename = cmd.split(' ')[1] print(filename) current_dir = current_dir + '//' + filename print(current_dir) if os.path.isdir(current_dir): send_data(conn,current_dir) else: conn.send('不是一个文件夹名'.encode('utf-8')) conn.close() sk.close()
Client:
import socket import os sk = socket.socket() sk.connect_ex(('127.0.0.1', 8080)) abs_path = input('请输入您的根目录>>>') sk.send(abs_path.encode('utf-8')) current_dir = sk.recv(1024).decode('utf-8') print(current_dir.split('__')) while 1: cmd = input('>>>') if== cmd ' .. ' : sk.send (cmd.encode ( ' UTF-. 8 ' )) current_dir = sk.recv (1024) .decode ( ' UTF-. 8 ' ) Print (current_dir.split ( ' __ ' ) ) elif cmd == ' CD ' : filename = iNPUT ( ' enter a folder name >>> ' ) sk.send ((cmd + ' ' + filename) .encode ( ' UTF-. 8 ')) current_dir = sk.recv(1024).decode('utf-8') print(current_dir.split('__')) sk.close()