#!/usr/bin/env python# -*- coding: utf-8 -*-# @Date : 2015年12月23日 23:24:53# @Author : eddy (278298125@qq.com)# @Link : http://my.oschina.net/eddylinux# @Version : 1.0import socketimport timeimport osip = 'localhost'port = 8888s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 定义当前目录current_dir = os.getcwd()def recvfile(filename):print(filename)print("server ready, now client rece file ~~ ")f = open(filename, 'wb')while True:data = s.recv(4096)if data == 'EOF':print("recv file success! ")breakf.write(data)f.close()def sendfile(filename):print("server ready, now client sending file ~~~")f = open(filename,'rb')while True:#data = f.read(4096).encode('utf-8')data = f.read(4096)#转换byte#data = b''.join(data)if not data:breaks.sendall(data)f.close()time.sleep(1)s.sendall(b'EOF')print("send file success!")def confirm(s, client_command):s.send(client_command.encode('utf-8'))data = s.recv(4096)if data == b'ready':return Truetry:s.connect((ip,port))while True:client_command = input(">>")if not client_command:continueaction, filename = client_command.split()if action == 'put':if confirm(s, client_command):sendfile(filename)else:print("server get error!")elif action == 'get':if confirm(s,client_command):print(current_dir)print(filename)filename = os.path.join(current_dir, os.path.split(client_command)[1])#filename = current_dir + '/' + os.path.split(client_command)[1]#filename = current_dir + os.path.join() + os.path.split(client_command)[1]print(filename)recvfile(filename)else:print("server get error!")else:print("Command error!")except socket.error as e:print("get error as ",e)finally:s.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。