regall
http://forums.devshed.com/python-programming-11/sending-a-file-using-sockets-129281.html
Спасибо. И еще пару вопросов. В каком месте в этом скрипте указывать путь к файлу, который хотим выслать?
# USAGE: python FileSender.py
import sys, socket
HOST = ‘localhost’
CPORT = 9091
MPORT = 9090
FILE = sys.argv
cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cs.connect((HOST, CPORT))
cs.send(“SEND ” + FILE)
cs.close()
ms = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ms.connect((HOST, MPORT))
f = open(FILE, “rb”)
data = f.read()
f.close()
ms.send(data)
ms.close()
И в какоим месте в данном скрипте указать путь куда файл должен быть сохранён?
# USAGE: python FileReciever.py
import socket, time, string, sys, urlparse
from threading import *
#————————————————————————
class StreamHandler ( Thread ):
def __init__( this ):
Thread.__init__( this )
def run(this):
this.process()
def bindmsock( this ):
this.msock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
this.msock.bind(('', 9090))
this.msock.listen(1)
print ' Listening on port 9090'
def acceptmsock( this ):
this.mconn, this.maddr = this.msock.accept()
print ' Got connection from', this.maddr
def bindcsock( this ):
this.csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
this.csock.bind(('', 9091))
this.csock.listen(1)
print ' Listening on port 9091'
def acceptcsock( this ):
this.cconn, this.maddr = this.csock.accept()
print ' Got connection from', this.maddr
while 1:
data = this.cconn.recv(1024)
if not data: break
if data == “SEND”: this.filename = data
print ' Getting ready to receive “%s”' % this.filename
break
def transfer( this ):
print ' Starting media transfer for “%s”' % this.filename
f = open(this.filename,“wb”)
while 1:
data = this.mconn.recv(1024)
if not data: break
f.write(data)
f.close()
print ' Got “%s”' % this.filename
print ' Closing media transfer for “%s”' % this.filename
def close( this ):
this.cconn.close()
this.csock.close()
this.mconn.close()
this.msock.close()
def process( this ):
while 1:
this.bindcsock()
this.acceptcsock()
this.bindmsock()
this.acceptmsock()
this.transfer()
this.close()
#————————————————————————
s = StreamHandler()
s.start()