IP地址为argumen的函数调用

2024-10-02 12:30:04 发布

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

我正在开发一个scraper,它可以检查文件传输的进度,它可以根据IP地址的pickle列表进行操作。当一个文件完成时,我希望IP地址从IP的pickle列表中删除,并移动到一个单独的pickle列表中,“complete”。在

开始时间:

servers = {"10.10.10.1": "", "10.10.10.2": "", "10.10.10.3": ""}
skeys = servers.keys()
complete = []

def donewith(server):
    if server in skeys:
        complete.append("{0}".format(server))
        servers.pop("{0}".format(server))
        logging.info('Moved {0} to Complete list.'.format(server))
    else:
        logging.error('Unable to move \'{0}\' to Complete list.'.format(server))

期望结果:

^{pr2}$

这就是我真正得到的。在

donewith(10.10.10.1)
File "<stdin>", line 1
donewith(10.10.10.1)
                ^
SyntaxError: invalid syntax

或者用引号调用函数会产生一个TypeError requiring an integer.不太确定如何解决这个问题,因为它看起来是一个如此简单的问题

报价方案阐述:

def check(server):
    #print "Checking {0}".format(server)
    logging.info('Fetching {0}.'.format(server))
    response = urllib2.urlopen("http://"+server+"/avicapture.html")
    tall = response.read() # puts the data into a string
    html = tall.rstrip()
    match = re.search('.*In Progress \((.*)%\).*', html)
    if match:
        temp = match.group(1)
        results = temp
        servers[server] = temp
        if int(temp) >= 98 and int(temp) <= 99:
            abort(server)
            alertmail(temp, server)
            donewith(server)
            logging.info('{0} completed.'.format(server))
        return str(temp)
    else:
        logging.error('Unable to find progress for file on {0}.'.format(server))
        return None

此函数调用donewith()函数,如果该函数有引号,例如:donewith("server"),则该函数不起作用。在

示例:

def check(server):
     donewith("server")

def donewith(server)
     do_things.

check(server)

导致。。在

check(10.10.10.3)
             ^
SyntaxError: invalid syntax

总是在第三组数字中有零。。。在


Tags: toinfoformat列表ifserverloggingdef
3条回答

键是一个字符串。你应该这样做:

    donewith('10.10.10.1')

您想要的行为是不可能的,因为10.10.10.1不是一个字符串,而且python看到点.,它试图将其解析为float,但是float只能有一个小数部分,这就是为什么异常指向第二个点之后的数字的原因。在

要使其工作,您需要将参数作为字符串传递:

donewith("10.10.10.1")

servers = {"10.10.10.2": "", "10.10.10.3": ""}
complete = ["10.10.10.1"]

不仅在donewith中,而且在{}中:

^{pr2}$

因为python就是这样工作的,所以需要用引号声明字符串,否则它将被视为数字(第一个字符是数字)或变量名。在

试试这个

In [6]: def donewith(server):
...:         if server in skeys:
...:                 complete.append(server)
...:                 servers.pop(server)
...:                 logging.info('Moved {0} to Complete list.'.format(server))
...:         else:
...:                 logging.error('Unable to move \'{0}\' to Complete list.'.format(server))
...:

In [7]: donewith("10.10.10.1")

In [8]:

In [8]: servers
Out[8]: {'10.10.10.2': '', '10.10.10.3': ''}

相关问题 更多 >

    热门问题