0

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.

Lucas Lisboa
  • 85
  • 1
  • 13
  • 2
    "parameters (if I'm not wrong, this is the same thing as an argument)" — not quite. Arguments are what a caller supplies; parameters are what the called function (or shell program, etc.) gets. So if you `def spam(x): return x+1`, that `x` is a parameter of `spam`; if you then `print(spam(1))`, that `1` is an argument to `spam`. Usually it doesn't matter if you're sloppy, but it's useful to know the distinction. (It gets very hard to talk about the two uses of `*args` otherwise, for example.) – abarnert Mar 19 '18 at 22:56
  • Possible duplicate of [What's the best way to grab/parse command line arguments passed to a Python script?](https://stackoverflow.com/questions/20063/whats-the-best-way-to-grab-parse-command-line-arguments-passed-to-a-python-scri) – Taku Mar 19 '18 at 23:30

1 Answers1

0

You can access the command line arguments using sys.argv, which is a list of the arguments passed to the script. Here's an example:

import sys
print(sys.argv)

Save this to test.py then execute this program with this command line:

# python test.py file.txt
['test.py', 'file.txt']

The first script argument is the name of the python script itself. The second is the first argument. You can use this in your client.py script to set the output file name:

import sys

if len(sys.argv) < 2:
    print('Usage: {} output_filename'.format(sys.argv[0]), file=sys.stderr)
    sys.exit(1)

output_filename = sys.argv[1]
with open(output_filename, 'wb') as arquivo:
    ...

The length of sys.argv is used to ensure that at least 2 arguments are present; the first being the script name, the second the output filename. Any additional arguments would simply be ignored.

mhawke
  • 84,695
  • 9
  • 117
  • 138