如何使用 Python 中的参数创建文件?
How to create a file using parameters in Python?
我目前正在创建一个文件传输客户端-服务器。我需要做的是让客户端使用参数创建一个文件(如果我没记错的话,这与参数是一样的)。所以,我要找的是 运行
python server.py
然后
python client.py file.txt
这是服务器代码:
# -*- 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()
这是客户端代码:
# -*- 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')
我正在寻找的是让客户端创建将发送到服务器的文件。
抱歉,葡萄牙语代码。如果有人需要更多说明,请询问。
您可以使用 sys.argv
访问命令行参数,这是传递给脚本的参数列表。这是一个例子:
import sys
print(sys.argv)
将此保存到 test.py
然后使用此命令行执行此程序:
# python test.py file.txt
['test.py', 'file.txt']
第一个脚本参数是 python 脚本本身的名称。第二个是第一个参数。您可以在 client.py
脚本中使用它来设置输出文件名:
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:
...
sys.argv
的长度用来保证至少有2个参数存在;第一个是脚本名称,第二个是输出文件名。任何额外的参数都将被忽略。
我目前正在创建一个文件传输客户端-服务器。我需要做的是让客户端使用参数创建一个文件(如果我没记错的话,这与参数是一样的)。所以,我要找的是 运行
python server.py
然后
python client.py file.txt
这是服务器代码:
# -*- 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()
这是客户端代码:
# -*- 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')
我正在寻找的是让客户端创建将发送到服务器的文件。
抱歉,葡萄牙语代码。如果有人需要更多说明,请询问。
您可以使用 sys.argv
访问命令行参数,这是传递给脚本的参数列表。这是一个例子:
import sys
print(sys.argv)
将此保存到 test.py
然后使用此命令行执行此程序:
# python test.py file.txt
['test.py', 'file.txt']
第一个脚本参数是 python 脚本本身的名称。第二个是第一个参数。您可以在 client.py
脚本中使用它来设置输出文件名:
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:
...
sys.argv
的长度用来保证至少有2个参数存在;第一个是脚本名称,第二个是输出文件名。任何额外的参数都将被忽略。