I'm currently creating a file transfer client-server. What I need to do is to make the client create a file using parameters (if I'm not wrong, this is the same thing as an argument). So, what I'm looking for is to run
python server.py
and then
python client.py file.txt
This is the server code:
# -*- coding: utf-8 -*-
import socket
from threading import Thread
class threaded_server(Thread):
def __init__(self,ip,port,sock):
Thread.__init__(self)
self.ip = ip
self.port = port
self.sock = sock
print " Nova conexão de "+ip+":"+str(port)
# rodando a thread, leitura da mensagem
def run(self):
arquivo = open('entrada.txt','rb')
while True:
temp = arquivo.read(msg)
while (temp):
self.sock.send(temp)
temp = arquivo.read(msg)
if not temp:
self.sock.close()
break
host = socket.gethostname()
port = 5001
msg = 1024
# não funcionou na declaração _def_init
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, porta))
threads = []
# servidor fica aguardando novas conexoes
while True:
sock.listen(4)
print "Aguardando..."
(conn, (ip,port)) = sock.accept()
print 'Conexão de: ', (ip,port)
newthread = threaded_server(ip,port,conn)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()
This is the client code:
# -*- coding: utf-8 -*-
import socket
host = socket.gethostname ()
port = 5001
msg = 2048
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
destino = (host, porta)
tcp.connect(destino)
with open('saida.txt', 'wb') as arquivo:
print 'Arquivo aberto'
while True:
dados = tcp.recv(msg)
if not dados:
arquivo.close()
break
arquivo.write(dados)
tcp.close()
print('Fim da Conexão')
What I'm looking for is to make the client create the file that it will be sent to the server.
Sorry for the code in portugese. If someone needs more clarification, just ask.