miketeo/pysmb

pysmb on iSeries and upload non txt files on remote server

jrios2006 opened this issue · 1 comments

Hi, I do not know if this is the web site for problem or doubts.

I have got an AS400 and I would like to use pysmb in AS400. In my AS400 I have installed python with http://www.iseriespython.com/

With this code

-- coding: UTF-8 --

import sys
import smb
from smb.SMBConnection import SMBConnection
from smb import smb_structs

def getBIOSName(remote_smb_ip, timeout=5):
# Devuelvo el nombre NetBios de una máquina remota
from nmb.NetBIOS import NetBIOS
Aux = 'ERROR'
try:
bios = NetBIOS()
srv_name = bios.queryIPForName(remote_smb_ip, timeout=timeout)
Aux = srv_name[0]
except:
print 'No es posible conocer el nombre NETBIOS del servidor ' + remote_smb_ip + ' en el tiempo ' + str(timeout)
finally:
bios.close()
return Aux

def ListaRecursosServidor(userID, password, local_ip, server_ip, domain_name):
# Devuelvo una lista en formato UTF-8 con los recursos disponibles via samba
# del servidor especificado con server_ip, con el nombre del dominio en domain_name
# y como autenticación el usuario userID y la contraseña password
# Si no conseguimos autenticarnos con el sistema remoto imprimos el error y damos una lista vacia
server_name = getBIOSName(server_ip, timeout=5)
client_machine_name = getBIOSName(local_ip, timeout=5)
#print domain_name
Aux = []
if (server_name == 'ERROR'):
print 'NO somos capaces de saber el nombre remoto por NETBIOS usamos su direccion Ip: ' + server_ip
server_name = server_ip
try:
#print userID, password, client_machine_name, server_name, domain_name
conn = SMBConnection(userID, password, client_machine_name, server_name, domain=domain_name, use_ntlm_v2=True,is_direct_tcp=True)
conn.connect(server_ip, 445)
shares = conn.listShares()
for share in shares:
if not share.isSpecial and share.name not in ['NETLOGON', 'SYSVOL','REPL$']:
Aux.append(share.name)
conn.close()
except:
print 'Error con el usuario: ' + userID + ' y la maquina: ' + server_ip
return Aux

def SubirFichero(userID, password, local_ip, server_ip, domain_name, Nombre_Recurso, path_destino, filename):
# Subo un fichero del IFS (filename) al sistema de ficheros remoto
# Servidor, Nombre_Recurso, path_destino, filename
import os
Aux = False
server_name = getBIOSName(server_ip, timeout=5)
client_machine_name = getBIOSName(local_ip, timeout=5)
print server_name + '/' + client_machine_name
if (server_name == 'ERROR'):
print 'No somos capaces de saber el nombre remoto(' + server_ip + ') por NETBIOS usamos su direccion Ip: ' + server_ip
server_name = server_ip
try:
conn = SMBConnection(userID, password, client_machine_name, server_name, domain=domain_name, use_ntlm_v2=True,is_direct_tcp=True)
except:
print 'Error al conectar. No pdemos subir con el usuario: ' + userID + ' a la maquina: ' + server_ip + ' el fichero ' + filename
if conn:
conn.connect(server_ip, 445)
print ''
try:
print 'Subimos en ' + Nombre_Recurso + path_destino + ' el fichero ' + filename
print 'Size = %.1f kB' % (os.path.getsize(filename) / 1024.0)
print 'start upload'
with open(filename, 'r') as file_obj:
print file_obj.name
print os.path.basename(file_obj.name)
print os.path.dirname(file_obj.name)
print '
***'
print path_destino+os.path.basename(file_obj.name)
filesize = conn.storeFile(Nombre_Recurso, path_destino+os.path.basename(file_obj.name), file_obj, timeout=100)
print 'upload finished'
Aux = True
except:
print 'No existe el fichero ' + filename
conn.close()
return Aux

I can upload txt file correctly, but when I try to upload to remote server a zip file, pdf file, the upload is OK but when I work with this file in other computer the zip file or pdf file are corrupted and I can not open them.

I try to review storefile method to understand the diferrent between txt file and others formats. Maybe AS400 is the problem.

Do you think any ideas to upload non txt file correctly in remote servers?
uncompress-zip

Best regards, Julio.

I modify the funcion SubirFichero
with open(filename, 'r') as file_obj:
and i changed
with open(filename, 'rb') as file_obj:

and it works,

Thanks for your software