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

本文介绍了一个使用 Python 的 socket 模块实现的简单文件传输系统。该系统包含服务端和客户端两部分,能够完成文件的上传和下载操作。通过自定义命令进行交互,实现了基本的文件传输功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值