Python socket实现文件传输的服务端与客户端

用python的socket模块实现的一个很简单的文件传输功能,不懂怎么写比较科学优雅,求各路大神指导

服务端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
#coding:utf-8
import  SocketServer
import  subprocess
import  string
import  time
class  MyTcpServer(SocketServer.BaseRequestHandler):
     def  recvfile( self , filename):
         print  "starting reve file!"
         =  open (filename,  'wb' )
         self .request.send( 'ready' )
         while  True :
             data  =  self .request.recv( 4096 )
             if  data  = =  'EOF' :
                 print  "recv file success!"
                 break
             f.write(data)
         f.close()
                                        
     def  sendfile( self , filename):
         print  "starting send file!"
         self .request.send( 'ready' )
         time.sleep( 1 )
         =  open (filename,  'rb' )
         while  True :
             data  =  f.read( 4096 )
             if  not  data:
                 break
             self .request.send(data)
         f.close()
         time.sleep( 1 )
         self .request.send( 'EOF' )
         print  "send file success!"
                                    
     def  handle( self ):
         print  "get connection from :" , self .client_address
         while  True :
             try :
                 data  =  self .request.recv( 4096 )
                 print  "get data:" , data  
                 if  not  data:
                     print  "break the connection!"
                     break               
                 else :
                     action, filename  =  data.split()
                     if  action  = =  "put" :
                         self .recvfile(filename)
                     elif  action  = =  'get' :
                         self .sendfile(filename)
                     else :
                         print  "get error!"
                         continue
             except  Exception,e:
                 print  "get error at:" ,e
                                            
                                        
if  __name__  = =  "__main__" :
     host  =  ''
     port  =  60000
     =  SocketServer.ThreadingTCPServer((host,port), MyTcpServer)
     s.serve_forever()



客户端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
#coding:utf-8
import  socket
import  sys
import  time
ip  =  '192.168.1.214'
port  =  60000
=  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def  recvfile(filename):
     print  "server ready, now client rece file~~"
     =  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~~"
     =  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, client_command):
     s.send(client_command)
     data  =  s.recv( 4096 )
     if  data  = =  'ready' :
         return  True
                                
try :
     s.connect((ip,port))
     while  1 :
         client_command  =  raw_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):
                 recvfile(filename)
             else :
                 print  "server get error!"
         else :
             print  "command error!"
except  socket.error,e:
     print  "get error as" ,e
finally :
     s.close()


运行效果:164258958.png


自娱自乐,好像挺有意思,哈哈。。。

本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1244632如需转载请自行联系原作者

lihuipeng
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
传输文件协议设计与实现是指服务端客户端之间传输文件的规则和实际操作。在Python中,可以使用socket模块来实现服务端: 1. 创建一个socket对象,绑定IP地址和端口号。 2. 使用socket的listen方法监听连接请求。 3. 当有连接请求到达时,使用socket的accept方法接受连接,并返回一个新的socket对象,用于处理该客户端的请求。 4. 通过该新的socket对象,可以使用recv方法接收客户端发送的文件名信息。 5. 根据文件名打开需要传输的文件,并将文件内容按照固定大小进行分块,然后使用send方法将文件块发送给客户端。 6. 重复步骤5直到文件传输完毕,然后关闭socket连接。 客户端: 1. 创建一个socket对象,指定服务端的IP地址和端口号。 2. 使用socket的connect方法连接到服务端。 3. 使用socket的send方法发送需要传输的文件名信息给服务端。 4. 使用recv方法接收服务端发送的文件块,并将文件块写入到本地文件中。 5. 重复步骤4直到文件传输完毕,然后关闭socket连接。 需要注意的是,在设计传输文件协议时,可以考虑使用一些特殊符号作为分隔符来区分不同的信息,比如使用换行符(\n)来分隔文件名和文件块。另外,还可以标记文件传输的起始和结束,以便在接收端正确处理文件数据。 上述是传输文件协议设计与实现的简单示例,可以根据具体需求进行修改和优化。传输文件的协议设计和实现需要考虑到数据的完整性、可靠性、性能等方面的问题,可以通过增加校验和、重传机制等来提高传输的可靠性和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值