Ai
2 Star 0 Fork 0

chobit/socket_file

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
clinet.py 1.26 KB
一键复制 编辑 原始数据 按行查看 历史
chobit 提交于 2016-07-07 21:57 +08:00 . socket test file
#!/usr/bin/python
#coding: utf-8
import socket
import time
ip = '127.0.0.1'
port = 60000
s = socket.socket()
def recvfile(filename):
print 'server ready, now clinet recv 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 clinet sending file--->'
f = open(filename,'rb')
while True:
data = f.read(4096)
if not data:
break
s.sendall(data)
f.close()
time.sleep(1)
s.sendall('EOF')
print 'send file success!'
def confirm(s,clinet_command):
s.send(clinet_command)
data = s.recv(4096)
if data =='read':
return True
try:
s.connect((ip,port))
while 1:
clinet_command = raw_input('>>')
if not clinet_command:
continue
action,filename = clinet_command.split()
if action == 'put':
path_filename = filename.split('/')[-1]
if confirm(s,clinet_command):
sendfile(path_filename)
else:
print 'server get error!'
elif action == 'get':
if confirm(s,clinet_command):
recvfile(filename)
else:
print 'server get error'
else:
print 'command error!'
except socket.error,e:
print 'get error as',e
finally:
s.close()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/chobit/socket_file.git
git@gitee.com:chobit/socket_file.git
chobit
socket_file
socket_file
master

搜索帮助