1. 程式人生 > >day029 Socketserver和 ftp

day029 Socketserver和 ftp

一.Socketserver

#服務端
import socketserver

class KnightServer(socketserver.BaseRequestHandler):

    def handle(self):
        # self.request  相當於conn連結通道
        while 1:
            from_client_msg = self.request.recv(1024)
            print(from_client_msg.decode('utf-8'))
            server_msg 
= input('服務端說>>>') self.request.send(server_msg.encode('utf-8')) if __name__ == '__main__': ip_port = ('127.0.0.1',8001) server = socketserver.ThreadingTCPServer(ip_port,KnightServer) server.serve_forever() #客戶端 import socket import time client = socket.socket() client.connect((
'127.0.0.1',8001)) while 1: client_data = input('別開車>>>') client.send(client_data.encode('utf-8')) from_server_msg = client.recv(1024) print(from_server_msg.decode('utf-8')) client.close()
socketserver

#ftp檔案上傳服務端

import socket
import time
import json
server = socket.socket()

server.bind(('127.0.0.1',8001))
server.listen()

conn,addr = server.accept()

def upload(conn,client_file_info):
#
upload_file_path = r'D:\jj' + '\\' + client_file_info['file_name']
recv_all_datalen = 0
with open(upload_file_path,'wb') as f:
while recv_all_datalen < client_file_info['file_size']:
every_recv_data = conn.recv(1024)
every_recv_datalen = len(every_recv_data)
recv_all_datalen += every_recv_datalen
f.write(every_recv_data)

print('客戶端上傳檔案')


client_userinfo = conn.recv(1024).decode('utf-8')
#定義一個需要返回給服務端的功能列表
server_function_list = {'1':'上傳','2':'下載'}
#將功能字典轉換為json字串,然後進行編碼,轉成bytes型別
json_func_list = json.dumps(server_function_list,ensure_ascii=False)


#服務端登入認證
with open('userinformation','r',encoding='utf-8') as f:
for i in f:
if i.strip() == client_userinfo:
#登入成功以後,將服務端能夠提供的功能,傳送給客戶端
conn.send(json_func_list.encode('utf-8'))
#接收客戶端的檔案描述資訊,其中有一項是功能序號
client_fileinfo_json = conn.recv(1024).decode('utf-8')
client_file_info = json.loads(client_fileinfo_json)

print(client_file_info)
if client_file_info['func_choice'] == '1':
upload(conn,client_file_info)

break
else:
#登入錯誤的資訊碼
conn.send(b'201')

 

 

 

 

#ftp 檔案上傳客戶端

import socket
import time
import json
server = socket.socket()

server.bind(('127.0.0.1',8001))
server.listen()

conn,addr = server.accept()

def upload(conn,client_file_info):
#
upload_file_path = r'D:\jj' + '\\' + client_file_info['file_name']
recv_all_datalen = 0
with open(upload_file_path,'wb') as f:
while recv_all_datalen < client_file_info['file_size']:
every_recv_data = conn.recv(1024)
every_recv_datalen = len(every_recv_data)
recv_all_datalen += every_recv_datalen
f.write(every_recv_data)

print('客戶端上傳檔案')


client_userinfo = conn.recv(1024).decode('utf-8')
#定義一個需要返回給服務端的功能列表
server_function_list = {'1':'上傳','2':'下載'}
#將功能字典轉換為json字串,然後進行編碼,轉成bytes型別
json_func_list = json.dumps(server_function_list,ensure_ascii=False)


#服務端登入認證
with open('userinformation','r',encoding='utf-8') as f:
for i in f:
if i.strip() == client_userinfo:
#登入成功以後,將服務端能夠提供的功能,傳送給客戶端
conn.send(json_func_list.encode('utf-8'))
#接收客戶端的檔案描述資訊,其中有一項是功能序號
client_fileinfo_json = conn.recv(1024).decode('utf-8')
client_file_info = json.loads(client_fileinfo_json)

print(client_file_info)
if client_file_info['func_choice'] == '1':
upload(conn,client_file_info)

break
else:
#登入錯誤的資訊碼
conn.send(b'201')

 

# 列印進度條

import time
for i in range(10):
str_p = i * '*'
print('\r'+ str_p,end='')
time.sleep(0.5)