Python下载Unraid的community.applications apps应用商店插件并上传到国内网站服务器

Python下载Unraid的community.applications apps应用商店插件并上传到国内网站服务器

需要在IIS中MIME 为.plg .txz 添加类型 application/octet-stream 这样打开下面文件时才会变成下载而非提示此文件不可打开

https://www.52help.net/ad/nas/Squidly271/community.applications/master/archive/community.applications-2020.06.13a-x86_64-1.txz

from urllib import request
from ftplib import FTP

blnver = False
blnname = False
blngithub = False
str_ver=""

替换文本函数

def alter(file,old_str,new_str):
file_data = ""
with open(file, "r", encoding="utf-8") as f:

for line in f:
  print(line)
  if old_str in line:
    line = line.replace(old_str,new_str)
  file_data += line

with open(file,"w",encoding="utf-8") as f:

f.write(file_data)



使用代理上网 才能连到unraid官网

proxies = {

'https': 'https://127.0.0.1:1080',
'http': 'http://127.0.0.1:1080'

}

需要加上headers, 否则报错: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 8974: invalid start byte

headers = {

'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'

}

community_url = 'https://raw.githubusercontent.com/Squidly271/community.applications/master/plugins/community.applications.plg'
opener = request.build_opener(request.ProxyHandler(proxies))
request.install_opener(opener)

req = request.Request(community_url, headers=headers)

f = request.urlopen(req)
data = f.read()
with open("community.applications.plg", "wb") as code:
code.write(data)

file_data = ""

获取文件中的软件名 版本号 路径

with open("community.applications.plg", "r", encoding="utf-8") as f:

for line in f:
  if not blnver:
     char_1='<!ENTITY version'
     if char_1 in line: 
        print("charfind")
        str_ver = line.replace(char_1,"")
        str_ver= str_ver.replace(" ","")
        str_ver= str_ver.replace(">","")
        str_ver= str_ver.replace('"','')
        str_ver= str_ver.replace('\n', '').replace('\r', '')
        print(str_ver)
        blnver = True 
  if not blnname:
     char_1='<!ENTITY name'
     if char_1 in line: 
        str_name = line.replace(char_1,"")
        str_name= str_name.replace(" ","")
        str_name= str_name.replace(">","")
        str_name= str_name.replace('"','')
        str_name= str_name.replace('\n', '').replace('\r', '')
        print(str_name)
        blnname = True  
  if not blngithub:
     char_1='<!ENTITY github'
     if char_1 in line: 
        str_github = line.replace(char_1,"")
        str_github= str_github.replace(" ","")
        str_github= str_github.replace(">","")
        str_github= str_github.replace('"','')
        str_github= str_github.replace('\n', '').replace('\r', '')
        print(str_github)
        blngithub = True  
  if "https://raw.githubusercontent.com/" in line:
    line = line.replace("https://raw.githubusercontent.com/","https://www.52help.net/ad/nas/")
  file_data += line

with open("community.applications.plg","w",encoding="utf-8") as f:

  f.write(file_data)

str_url=''
str_filename='&name;-&version;-x86_64-1.txz'
str_1='https://raw.githubusercontent.com/&github;/master/archive/' #&name;-&version;-x86_64-1.txz
str_url = str_1.replace("&github;",str_github)
str_filename = str_filename.replace("&name;",str_name)
str_filename = str_filename.replace("&version;",str_ver)
str_url =str_url + str_filename
print(str_url)

req = request.Request(str_url, headers=headers)
f = request.urlopen(req)
data = f.read()
with open(str_filename, "wb") as code:
code.write(data)

alter("community.applications.plg", "https://raw.githubusercontent.com/", "https://www.52help.net/ad/nas/")

将下载的文件修改后再上传到网站服务器

ftp = FTP()

打开调试级别2, 显示详细信息

ftp.set_debuglevel(2)

服务器IP和端口

ftp.connect("www.52help.net", 21)

匿名登陆, 如果需要登陆, 就把两个空字符串写上用户名和密码就行了("username", "password")

ftp.login("52help", "FTP登录密码")

切换目录, 相对于ftp目录, 比如设置的ftp根目录为/vat/ftp, 那么pub就是/var/ftp下面的目录

ftp.cwd("/52help/web/ad/nas/Squidly271/community.applications/master/plugins/")

查看目录下有哪些文件, 如果文件名已经存在, 那么再次上传同一个文件就会报错, 返回列表

ftp.nlst("/pub")

使用二进制的方式打开文件

f = open("community.applications.plg", 'rb')

上传文件, bufsize缓冲区大小

ftp.storbinary("STOR {}".format("community.applications.plg"), f [, bufsize])

ftp.storbinary('STOR %s' % "community.applications.plg", f)
f.close()

ftp.cwd("/52help/web/ad/nas/Squidly271/community.applications/master/archive/")
f = open(str_filename, 'rb')
ftp.storbinary('STOR %s' % str_filename, f)
f.close()

关闭调试模式

ftp.set_debuglevel(0)

退出FTP连接

ftp.quit()

上一篇:wordpress的主题和插件的上传安装


下一篇:使用Logging Handler自动上传Python程序日志到日志服务