Service side:
1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 " "4 Administrator5 2018/8/16 " "7 Importsubprocess8 ImportSocket,os9 Ten #Create a socket: One #family=af_inet, Type=sock_stream, proto=0, Fileno=none A #when creating a socket, af_inet specifies the use of the IPV4 protocol, - #If you want to use a more advanced IPv6, specify it as Af_inet6 - #Af_unix communication between different processes in UNIX the #Sock_stream Specifies the use of a stream-oriented TCP protocol SOCK_DGRAM UDP protocol -s=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) - Print(s) -Address= ('127.0.0.1', 8000) + #Establish a connection - S.bind (address) + #Listener set port waits for client's request number AS.listen (3) at - - Print('waiting for ... ..') -Base_dir=os.path.dirname (Os.path.abspath (__file__)) - - whileTrue: in #Accept blocking -conn, addr =s.accept () to Print(addr) + whileTrue: -DATA=CONN.RECV (1024) theCMD,FILENAME,FILESIZE=STR (data,"UTF8"). Split ("|") *Path=os.path.join (Base_dir,"img", filename) $Filesize=Int (filesize)Panax Notoginseng -F=open (Path,"WB") the +Get_sent=0 A whileGet_sent! =FileSize: theDATA=CONN.RECV (1024) +get_sent+=len (data) - f.write (data) $ Print("acceptance is complete. ") $ f.close () - - theS.close ()
Client:
1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 " "4 Administrator5 2018/8/16 " "7 ImportSocket,os,sys8 9SCK =Socket.socket (socket.af_inet, socket. SOCK_STREAM)Ten Print(SCK) OneAddress = ('127.0.0.1', 8000)#192.168.0.103 A Sck.connect (address) - -Base_dir=os.path.dirname (Os.path.abspath (__file__)) the whileTrue: -INP = input (">>>"). Strip ()#post|kanqingzi.jpg - -Cmd,path=inp.split ("|") +Path=os.path.join (Base_dir,path)#Stitching Path - +Filename=os.path.basename (PATH)#need file name AFilesize=os.stat (path). st_size#File size Required at #File Packaging -file_info="post|%s|%s"% (filename,filesize)#string concatenation -Sck.sendall (Bytes (File_info,"UTF8")) - -With open (path,"RB") as F: -Has_sent=0 in whileHas_sent! =FileSize: -Data=f.read (1024) to sck.sendall (data) +has_sent+=len (data) - Print("upload success!!! ") the *Sck.close ()
Exercise: Remote control of the other computer, and the other computer inside the pictures sent over.
Python Entry 28th day-File Upload