Search code examples
pythontelnetlib

uploading file via telnet


Im writing a python scrpit to upload files to my file server:

host = "myhost.dev"
tn = telnetlib.Telnet()
tn.open(host, 5202)
print tn.read_until("\n")
fp = "./output"

f = open(fp, "r")
f_body = f.read()

tn.write(f_body)
tn.write("\n")

f.close()

If file has a new line character- 0a, and it is part of a binary data in gzip file, what should I do to escape it ? Can python telnetlib do it by itself ? or should I do it ?

best regards


Solution

  • I think that telnet is not the best option for transfering files, but if you still want to use it for uploading files. You may try to do the following (haven't tried, but I think should work)

    #On client side
    ...
    import base64
    with open('test.gz','rb') as f:
        content = f.read()
    
    content_serialized = base64.b64encode(content)+'\n'
    ...
    #On server side
    ...
    import base64
    content = base64.b64decode(content_serialized.rstrip('\n'))
        with open('test.gz','wb') as f:
            f.write(content)    
    ...