从2个函数到新函数的2个响应

2024-10-03 21:24:08 发布

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

我正在编写一小段代码,将保存的文档中的web服务器页面的etag与服务器上的etag进行比较。如果它们不同,代码将指示这一点。我的密码是下图:-你知道吗

import httplib

def currentTag():
    f = open('C:/Users/ME/Desktop/document.txt')
    e = f.readline()
    newTag(e)

def newTag(old_etag):
    c = httplib.HTTPConnection('standards.ieee.org')
    c.request('HEAD', '/develop/regauth/oui/oui.txt')
    r = c.getresponse()
    current_etag = r.getheader('etag').replace('"', '')
    compareTag(old_etag, current_etag)

def compareTag(old_etag, current_etag):
    if old_etag == current_etag:
        print "the same"
    else:
        print "different"

if __name__ == '__main__':
    currentTag()

现在,回顾一下我的代码,实际上没有理由将“etag”从currentTag()方法传递到newTag()方法,因为在newTag()中没有处理预先存在的etag。尽管如此,如果我不这样做,我怎么能把两个不同的值传递给compareTag()。例如,在定义compareTag()时,如何从currentTag()方法传递“etag”和从newTag()方法传递“current”呢?你知道吗


Tags: 方法代码服务器txtifdefcurrentold
3条回答
def checkTags():
    c = httplib.HTTPConnection('standards.ieee.org')
    c.request('HEAD', '/develop/regauth/oui/oui.txt')
    r = c.getresponse()
    with open('C:/Users/ME/Desktop/document.txt', 'r') as f:
        if f.readline() == r.getheader('etag').replace('"', ''): print "the same"
        else: print "different"

将主菜单更改为:

if __name__ == '__main__':
    compareTag(currentTag(), newTag())

然后让currentTag()返回e和newTag()返回当前的etag

你不应该像这样链接你的函数调用,有一个连续调用函数的主要代码块,比如:

if __name__ == '__main__':
  currtag = currentTag()
  newtag = newTag()
  compareTag(currtag,newtag)

调整函数以返回相关数据

函数的基本思想是返回数据,通常使用函数进行一些处理并返回值,而不是用于控制流。你知道吗

相关问题 更多 >