简体   繁体   中英

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

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)    
...

Maybe telnet is not the best solution for this, it is better to use FTP o HTTP. The telnet protocol is not suitable for transmiting files, and it has some control flow processes that make if dificult to transmit files and special chars. If you want to use a non standard protocol, it is better so use the socket module, with sockets you don't have this problems with 0a.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM