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 gz开发者_Go百科ip 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.
精彩评论