1 Star 0 Fork 0

Coing/code-sample

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
socket-file-cli.py 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
Coing 提交于 2016-06-14 19:32 +08:00 . add work python
#!/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.0
import socket
import time
import os
ip = 'localhost'
port = 8888
s = 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! ")
break
f.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:
break
s.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 True
try:
s.connect((ip,port))
while True:
client_command = input(">>")
if not client_command:
continue
action, 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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Coing/code-sample.git
git@gitee.com:Coing/code-sample.git
Coing
code-sample
code-sample
master

搜索帮助