Upload file via network with Python

2

Server

#!/usr/bin/env python
#-*-coding:utf-8;-*-
import socket
import os
host = "127.0.0.1"
port = 8080
addr = (host, port)
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
skt.bind(addr)
skt.listen(1)
print "||-----------------------------------------------------------||"
print "||---                      Link v1.0                      ---||"
print "||-----------------------------------------------------------||"
print "Aguardando conexão..."
dd, cnx = skt.accept()
print "Conectado ao host: ", cnx
opc = dd.recv(1024)
if opc == "0":
  skt.close()
  exit()
elif opc == "1":
  name = dd.recv(1024)
  fli = dd.recv(1024)
  with open(fli, "rb") as flo:
    flo.write(name.read())

Customer

#!/usr/bin/env python
#-*-coding:utf-8;-*-
import socket
import os
print "||-------------------------------------------------------||"
print "||---                    Link v1.0                    ---||"
print "||-------------------------------------------------------||"
host = "127.0.0.1"
port = 8080
addr = (host, port)
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
skt.connect(addr)
print "[1]=> Enviar\n[2]=> Baixar\n[0]=> Sair"
opc = raw_input("[Opção]=> ")
if opc == "0":
  skt.send("0")
  skt.close()
  exit()
elif opc == "1":
  skt.send("1")
  print "Deseja listar diretorios?"
  op = raw_input("s/N: ")
  if op == "s" or op == "S":
    d = raw_input("[Dir]=> ")
    dr = os.listdir(d)
    for o in dr:
      print o
    fli = raw_input("Open file: ")
    name = raw_input("Name: ")
    skt.send(name)
    with open(fli, "rb") as flo:
      skt.send(flo.read())
      print "\n" * 200
      print "Done!"

The error this is:

TypeError: file() argument 1 must be encoded string without null bytes, not str
    
asked by anonymous 23.08.2018 / 18:54

0 answers