Python查找IP并上传到FTP

2024-10-03 11:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试编写一个简单的脚本来获取我的ip并将其上载到ftp服务器。我可以这样做,如果我只使用一个简单的字符串,但我很难上传IP地址。我在想

def testftp():
filename="testing1.txt"  #know this should all be indented
targetfile=open(filename, 'a')

#get my ip, dont think this is the way to do it, but it works in interactive anyways
getmyip=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
getmyip.connect(("google.com", 80))
print(getmyip.getsockname()[0])

targetfile.write("Your ip address is: ")
targetfile.write(getip)
tragetfile.write('\n')

session = ftplib.FTP('ftp.com', 'user', 'pass')
file = open('testing1.txt', 'rb')
session.storbinary('STOR testing1.txt', file)
file.close()        
session.quit()

testftp() 

我得到了一个“TypeError:expected a character buffer object”错误,我已经在google上搜索过了,但是我似乎找不到任何特定于这个例子的东西。嗯。所以我遇到了麻烦,以为我终于在这里发帖了。希望有人能帮忙。。提前谢谢。在


Tags: iptxtissessionftpsocketopenfilename