如何将html文件加载到远程计算机

2024-06-11 08:01:46 发布

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

我想把一个HTML文件加载到远程机器上。我怎么能这么做?对于JSON我们有json.dump,对于html我们有类似的东西吗?我正在使用这个:这是我的脚本:

import unittest
from time import sleep 
from Support import 
ucepTestCase,ucepServicesConfig,UCEPServicesAPIOperations
from Support.page.UCEP import EmptyPage
from bs4 import BeautifulSoup
import glob
import os
import re
import contextlib
import urllib
import bs4


class Test(ucepTestCase.UCEPTestCase):
user_id = None
sp = None
device_data = None

def test(self):

    #Export all Branding files            
    service = ucepServicesConfig.presentation
    files = self.l_ops.export_branding_files()
    print files
    #Download the Footer files and the logo from remote path to local path.
    ExternalFooter =self.l_ops.download_branding(filename="ext.footer.html") 
    print ExternalFooter
    InternalFooter =self.l_ops.download_branding(filename="int.footer.html")
    print InternalFooter
    Logo = self.l_ops.download_branding(filename="broadsoft_ucone.png")


    #Making Changes in the external footer file  
    with open('local_ext.footer.html', 'w') as data_file:
        data_file.write('<div layout="row">'
                         '<div flex="33" style="margin-left: 15px; margin-top: 13px;">'
                         '<div aria-label="broadsoft_ucone.png" style="height:40px; background-image:url(/ucassets/img/broadsoft_ucone.png); background-repeat: no-repeat; background-size:contain"></div>'
                         '</div>'
                         '<div flex="66" style="text-align: center; margin-top: 20px;">All Rights Reserved.</div>'
                         '<div flex="33"><!-- balance logo --></div>'
                         '</div>')



    #Making Changes in the internal footer file  
    with open('local_int.footer.html', 'w') as data_file:
        data_file.write('<div layout="row">'
                         '<div flex="33" style="margin-left: 15px; margin-top: 13px;">'
                         '<div aria-label="broadsoft_ucone.png" style="height:40px; background-image:url(/ucassets/img/broadsoft_ucone.png); background-repeat: no-repeat; background-size:contain"></div>'
                         '</div>'
                         '<div flex="66" style="text-align: center; margin-top: 20px;">All Rights Reserved.</div>'
                         '<div flex="33"><!-- balance logo --></div>'
                         '</div>')



    #  Pushing the html files and logo from local path to remote path
    self.l_ops.push_branding_config(ExternalFooter,"local_ext.footer.html", service, restart = True)
    self.l_ops.push_branding_config(InternalFooter, "local_int.footer.html", service, restart = True)
    self.l_ops.push_branding_config(Logo, "local_broadsoft_ucone.png", service, restart = True)
    sleep(20) 

    empty_page = EmptyPage.EmptyPage()
    login_page = empty_page.open_login_page()
    welcome_page = login_page.login(min_level="ent", max_level="ent")








if __name__ == '__main__':
unittest.main()        

我在脚本中使用的函数有: 在这里,我认为我在“加载本地html”中犯了一些错误。。有没有命令将html文件加载到远程服务器

''' Exporting the Branding Footer files and the Logo '''
def export_branding_files(self):
    logging.info(self.LOGNAME + "export branding files and save to export_qaauto/ path")
    service = "presentation"

    self.file_ops.exec_ssh_command("cd " + self.ucep_dir +";mkdir -p export_qaauto")

    self.file_ops.exec_ssh_command("cd " + self.ucep_dir + "; ./portalcfg.sh " + service + " --export-file 'presentation.branding.int.footer.html'  > export_qaauto/int.footer.html"  , returnoutput=True)
    self.file_ops.exec_ssh_command("cd " + self.ucep_dir + "; ./portalcfg.sh " + service + " --export-file 'presentation.branding.ext.footer.html'  > export_qaauto/ext.footer.html"  , returnoutput=True)
    self.file_ops.exec_ssh_command("cd " + self.ucep_dir + "; ./portalcfg.sh " + service + " --export-file 'presentation.branding.broadsoft_ucone.png'  > export_qaauto/broadsoft_ucone.png"  , returnoutput=True)
    time.sleep(2)

''' downloading the Branding Footer files and the Logo to the local path '''   
def download_branding(self, filename):
    logging.info(self.LOGNAME + "downloading "+ filename +" file")

    filepath = self.ucep_dir
    localpath = "./local_"+ filename
    self.file_ops.download_remote_file_to_a_local_machine(filepath + "/export_qaauto/" + filename ,localpath)
    config = self.load_local_html(localpath)
    return config    

def load_local_html(self, html_file):
    logging.info(self.LOGNAME + "Convert " + html_file + " to the python dict")
    # Edit file

    with open(html_file, "wb") as outfile:
        outfile.write('<div layout="row">'
                         '<div flex="33" style="margin-left: 15px; margin-top: 13px;">'
                         '<div aria-label="broadsoft_ucone.png" style="height:40px; background-image:url(/ucassets/img/broadsoft_ucone.png); background-repeat: no-repeat; background-size:contain"></div>'
                         '</div>'
                         '<div flex="66" style="text-align: center; margin-top: 20px;">All Rights Reserved.</div>'
                         '<div flex="33"><!-- balance logo --></div>'
                         '</div>')
    return outfile

def save_local_html(self, html_file, html_obj, config_name=None):
    logging.info(self.LOGNAME + "Save html to " + html_file)
    if config_name is not None:
        self.services_configs[config_name] = html_obj
    # Edit file
    with open(html_file, "wb") as outfile:
        outfile.write('<div layout="row">'
                         '<div flex="33" style="margin-left: 15px; margin-top: 13px;">'
                         '<div aria-label="broadsoft_ucone.png" style="height:40px; background-image:url(/ucassets/img/broadsoft_ucone.png); background-repeat: no-repeat; background-size:contain"></div>'
                         '</div>'
                         '<div flex="66" style="text-align: center; margin-top: 20px;">All Rights Reserved.</div>'
                         '<div flex="33"><!-- balance logo --></div>'
                         '</div>')    

''' Pushing the branding files from local path to remote path'''        
def push_branding_config(self, config, branding,service,restart=False):
    self.save_local_html(service.local_service_config, config, service.name)


    if(branding=="ext.footer.html"):
        self.file_ops.send_local_file_to_a_remote_machine(service.local_service_config,
                                                      self.ucep_dir + service.remote_ext_footer_html )
        self.file_ops.exec_ssh_command(
        "chown " + ucepServicesConfig.services_docker_user + ":" + ucepServicesConfig.services_docker_group + " " +
        self.ucep_dir + service.remote_ext_footer_html)
        self.file_ops.exec_ssh_command(
        "chmod 644 " + self.ucep_dir + service.remote_ext_footer_html)
        now = time.time()
        if restart:
            self.restart_branding_service(service.name, "ext_footer_html_auto.html" , branding=["ext.footer.html"])
        #print "{0:.1f}".format(time.time() - now)
    elif(branding == "int.footer.html"):
        self.file_ops.send_local_file_to_a_remote_machine(service.local_service_config,
                                                      self.ucep_dir + service.remote_int_footer_html )
        self.file_ops.exec_ssh_command(
        "chown " + ucepServicesConfig.services_docker_user + ":" + ucepServicesConfig.services_docker_group + " " +
        self.ucep_dir + service.remote_int_footer_html)
        self.file_ops.exec_ssh_command(
        "chmod 644 " + self.ucep_dir + service.remote_int_footer_html)
        now = time.time()
        if restart:
            self.restart_branding_service(service.name, "int_footer_html_auto.html", branding=["int.footer.html"])
        #print "{0:.1f}".format(time.time() - now)

    elif(branding == "broadsoft_ucone.png"):
        self.file_ops.send_local_file_to_a_remote_machine(service.local_service_config,
                                                      self.ucep_dir + service.remote_broadsoft_ucone_png )
        self.file_ops.exec_ssh_command(
        "chown " + ucepServicesConfig.services_docker_user + ":" + ucepServicesConfig.services_docker_group + " " +
        self.ucep_dir + service.remote_broadsoft_ucone_png)
        self.file_ops.exec_ssh_command(
        "chmod 644 " + self.ucep_dir + service.remote_broadsoft_ucone_png)
        now = time.time()
        if restart:
            self.restart_branding_service(service.name, "broadsoft_ucone.png", branding=["broadsoft_ucone.png"])
        #print "{0:.1f}".format(time.time() - now)   

def restart_branding_service(self, service, config="service.json", branding=[]):
    logging.info(self.LOGNAME + "Restart Service: " + service)
    self.import_branding_config(service, config, branding)
    self.file_ops.exec_ssh_command("cd " + self.ucep_dir + "; docker-compose restart " + service)
    time.sleep(10)       

def import_branding_config(self, service_name, config_name, branding=[]):
    if service_name == "presentation":
        if "ext.footer.html" in branding:
            output = self.file_ops.exec_ssh_command("cd " + self.ucep_dir + "; ./portalcfg.sh presentation --import-file ""presentation.branding.ext.footer.html"" < ./" + service_name + "/config/" + config_name, returnoutput=True)
            self.verify_values_are_equal("Command executed successfully\n", output[0])
        elif "int.footer.html" in branding:
            output = self.file_ops.exec_ssh_command("cd " + self.ucep_dir + "; ./portalcfg.sh presentation --import-file ""presentation.branding.int.footer.html"" < ./" + service_name + "/config/" + config_name, returnoutput=True)
            self.verify_values_are_equal('Command executed successfully\n', output[0])
        elif "broadsoft_ucone.png" in branding:
            output = self.file_ops.exec_ssh_command("cd " + self.ucep_dir + "; ./portalcfg.sh presentation --import-file ""presentation.branding.broadsoft_ucone.png"" < ./" + service_name + "/config/" + config_name, returnoutput=True)
            self.verify_values_are_equal("Command executed successfully\n", output[0])
        else:
            filename = service_name+"_service_auto.html"
            output = self.file_ops.exec_ssh_command("cd " + self.ucep_dir + "; ./portalcfg.sh presentation --import-config < export_qaauto/"+filename, returnoutput=True)
            self.verify_values_are_equal("Command executed successfully\n", output[0])        
    else:
        filename = service_name+"_service_auto.html"
        output = self.file_ops.exec_ssh_command("cd " + self.ucep_dir + "; ./portalcfg.sh "+ service_name +" --import-config < export_qaauto/"+filename, returnoutput=True)
        self.verify_values_are_equal("Command executed successfully\n", output[0])  

Tags: nameselfdivconfigpnglocalhtmlservice