Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python编写此代码的更好方法_Python_Urllib2 - Fatal编程技术网

用Python编写此代码的更好方法

用Python编写此代码的更好方法,python,urllib2,Python,Urllib2,我是python的新手,还在不断学习 我有一个Web服务器,其中包含要加载到被测设备(DUT)上的图像列表 要求是: 如果服务器上已存在映像,则继续将映像加载到DUT上 如果服务器上没有映像,则继续下载映像,然后升级DUT 我已经写了下面的代码,但是我对我写这段代码的方式很不满意,因为我觉得使用其他方法可以做得更好 请建议我本可以做得更好的领域和技巧 感谢您花时间阅读此电子邮件并提出宝贵建议 import urllib2 url = 'http://localhost/test' filena

我是python的新手,还在不断学习

我有一个Web服务器,其中包含要加载到被测设备(DUT)上的图像列表

要求是:

如果服务器上已存在映像,则继续将映像加载到DUT上

如果服务器上没有映像,则继续下载映像,然后升级DUT

我已经写了下面的代码,但是我对我写这段代码的方式很不满意,因为我觉得使用其他方法可以做得更好

请建议我本可以做得更好的领域和技巧

感谢您花时间阅读此电子邮件并提出宝贵建议

import urllib2

url = 'http://localhost/test'
filename = 'Image60.txt'   # image to Verify

def Image_Upgrade():
    print 'proceeding with Image upgrade !!!'

def Image_Download():
    print 'Proceeding with Image Download !!!'

resp = urllib2.urlopen(url)
flag = False
list_of_files = []
for contents in resp.readlines():
    if 'Image' in contents:
        c=(((contents.split('href='))[-1]).split('>')[0]).strip('"')  # The content output would have html tags. so removing the tags to pick only image name
        if c != filename:
            list_of_files.append(c)
        else:
            Image_Upgrade()
            flag = True
if flag==False:
    Image_Download()
谢谢,
Vijay Swaminathan属于codereview.stackexchange。com@Wooble有什么原因你没有投票关闭吗?@Wooble,很抱歉我没有收到你的评论。。你能不能再多说一点???@user596922:堆栈溢出是针对特定的编程问题;“我怎样才能改进这段代码,使其工作起来”这句话太笼统了,但非常适合代码审查网站。我只是觉得我从http服务器获取文件名的方法(在本例中,使用href等拆分)是无效的,应该有其他方法来获取http服务器上存在的文件名。所以我想知道这是否可以做到?