기본 콘텐츠로 건너뛰기

[python] sending a file over sockets

source: http://forums.devshed.com/python-programming-11/sending-file-using-sockets-129281.html

question: Does anyone know how to properly send a file using sockets? At the moment I am using this:

answer:

  1 # USAGE: python FileSender.py [file]
  2
  3 import sys, socket
  4
  5 HOST = 'localhost'
  6 CPORT = 9091
  7 MPORT = 9090
  8 FILE = sys.argv[1]
  9
 10 cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 11 cs.connect((HOST, CPORT))
 12 cs.send("SEND " + FILE)
 13 cs.close()
 14
 15 import time
 16 time.sleep(1)
 17 ms = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 18 ms.connect((HOST, MPORT))
 19
 20 f = open(FILE, "rb")
 21 data = f.read()
 22 f.close()
 23
 24 ms.send(data)
 25 ms.close()


  1 # USAGE: python FileReciever.py
  2
  3 import socket, time, string, sys, urlparse
  4 from threading import *
  5
  6 MPORT=9090
  7 CPORT=9091
  8
  9 #------------------------------------------------------------------------
 10
 11 class StreamHandler ( Thread ):
 12
 13     def __init__( this ):
 14         Thread.__init__( this )
 15
 16     def run(this):
 17         this.process()
 18
 19     def bindmsock( this ):
 20         this.msock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 21         this.msock.bind(('', MPORT))
 22         this.msock.listen(1)
 23         print '[Media] Listening on port MPORT'
 24
 25     def acceptmsock( this ):
 26         this.mconn, this.maddr = this.msock.accept()
 27         print '[Media] Got connection from', this.maddr
 28
 29     def bindcsock( this ):
 30         this.csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 31         this.csock.bind(('', CPORT))
 32         this.csock.listen(1)
 33         print '[Control] Listening on port %d' %CPORT
 34
 35     def acceptcsock( this ):
 36         this.cconn, this.maddr = this.csock.accept()
 37         print '[Control] Got connection from', this.maddr
 38
 39         while 1:
 40             data = this.cconn.recv(1024)
 41             if not data: break
 42             if data[0:4] == "SEND": this.filename = data[5:]
 43             print '[Control] Getting ready to receive "%s"' % this.filename
 44             break
 45
 46     def transfer( this ):
 47         print '[Media] Starting media transfer for "%s"' % this.filename
 48
 49         f = open(this.filename,"wb")
 50         while 1:
 51             data = this.mconn.recv(1024)
 52             import time; time.sleep(0.3)
 53             print(data)
 54             if not data: break
 55             f.write(data)
 56         f.close()
 57
 58         print '[Media] Got "%s"' % this.filename
 59         print '[Media] Closing media transfer for "%s"' % this.filename
 60
 61     def close( this ):
 62         this.cconn.close()
 63         this.csock.close()
 64         this.mconn.close()
 65         this.msock.close()
 66
 67     def process( this ):
 68         this.bindcsock()
 69         this.acceptcsock()
 70
 71         this.bindmsock()
 72         this.acceptmsock()
 73         this.transfer()
 74         this.close()

댓글