#!/usr/bin/env python
import commands
import time
import SocketServer
class MyTCPHandler(SocketServer.BaseRequestHandler):
defrecv_all(self,obj,msg_length,des_file):
while msg_length!=0:
if msg_length<=4096:
data =obj.recv(msg_length)
msg_length= 0
else:
data =obj.recv(4096)
msg_length-=4096
des_file.write(data)
return 'Done'
def handle(self):
print 'Got anew connection from',self.client_address
while True:
cmd = self.request.recv(1024).strip()
if not cmd:
print 'Lostconnection with',self.client_address
break
option,filename,file_size = cmd.split()
if option == 'put':
#clientwants to upload file
f=file('recv/%s' %filename,'wb')
write_to_file = self.recv_all(self.request,int(file_size),f)
ifwrite_to_file == 'Done':
self.request.send('File uploaded success')
f.close()
if __name__ == "__main__":
HOST,PORT="192.168.2.110",90
server =SocketServer.ThreadingTCPServer((HOST,PORT),MyTCPHandler)
server.serve_forever()