2017-11-17 8 views
1

파이썬 들여 쓰기에 대한 적절한 프로토콜을 따르려고하지만 여전히 파이썬에서 오류가 발생합니다. 나는 파이썬이 좋은 이유가 있고 내 코드가 나쁜 것이라고 확신하지만 근본 원인을 보지 못했습니다. 오류를 실행 Python 들여 쓰기가 직관적이지 않습니다.

은 마지막 줄

~/python $ ./hover_api_v1.0.py 
    File "./hover_api_v1.0.py", line 139 
    time.sleep(60.0) 
    ^
IndentationError: expected an indented block 

다음은 내 코드는 지적한다. 들여 쓰기와 관련없는 일부 헤더가 있습니다.

오류는 time 명령을 사용하여 마지막 행에 표시됩니다. 그러나 나는 코드에서 내 실수를 보지 못한다. 시간 cmd는 최상위 루프의 일부이고 적절히 들여 쓰기됩니다.

while True: 
    ip_now = get_asus_wan_ip() 
    if (ip_now == ip_last): 
     day = datetime.datetime.now().day 
     if day != last_day: 
      last_day = day 
      with open(logfile, "a") as lf: 
       lf.write(time.strftime("%Y-%m-%d %H:%M:%S") + " WAN IP still the same " + str(ip_last) +"\n") 

    else: 
     # We need to do something 
     #print('WAN IP changed from ' + str(ip_last) + " to " + str(ip_now)) 
     with open(logfile, "a") as lf: 
      lf.write(time.strftime("%Y-%m-%d %H:%M:%S") + " WAN IP changed from " + str(ip_last) + " to " + str(ip_now) + "\n") 
     ip_last= ip_now 

     # connect to API 
     client = HoverAPI('XXXXXXX','YYYYYYY') 
     for dnsname in ['*.zzzzz.zzz', '@.zzzzz.zzz']: 
      #print('Testing: ' + dnsname) 
      dns_name, domain_name = dnsname.split('.', 1) 

      # get all DNS records 
      result = client.call("get", "dns") 
      assert result['succeeded'], result 

      # discover existing dns record, if any 
      dns_record = None 
      domain_record = None 
      for dns_domain in result['domains']: 
       if dns_domain['domain_name'] == domain_name: 
        domain_record = dns_domain 
        for dns_entry in dns_domain['entries']: 
         if dns_entry['name'] == dns_name: 
          dns_record = dns_entry 
          break 
       if dns_record is not None and domain_record is not None: 
        break 

      if dns_record is not None and domain_record is not None: 
       #print('Hover-IP for ' + dnsname + ' = ' + str(dns_entry['content'].encode('ascii','ignore'))) 
       #print('Current IP= ' + str(ip_now)) 
       if str(dns_entry['content']) == str(ip_now): 
        #print('Hover-IP for ' + dnsname + ' = ' + str(dns_entry['content'].encode('ascii','ignore')) + ' same as Current IP = ' + str(ip_now) + '. No action.') 
        with open(logfile, "a") as lf: 
         lf.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Hover-IP for " + dnsname + " = " + str(dns_entry['content'].encode('ascii','ignore')) + " same as Current IP = " + str(ip_now) + ". No action." + "\n") 
       else: 
        #print(" Deleting entry for {0}.{1} ... ".format(dns_name, domain_name), end="") 
        with open(logfile, "a") as lf: 
         lf.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Deleting entry for " + dnsname + "\n") 
        result = client.call("delete", "dns/{0}".format(dns_record['id'])) 
        assert result['succeeded'], result 
        #print("OK") 
        ## create a new A record: 
        #print("Creating A record {0}.{1} => {2} ... ".format(dns_name, domain_name, ip_now), end="") 
        with open(logfile, "a") as lf: 
         lf.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Creating A record " + dnsname + " => " + ip_now + "\n") 
        record = {"name": dns_name, "type": "A", "content": ip_now} 
        post_id = "domains/{0}/dns".format(domain_record['id']) 
        #print("post", post_id, record) 
        result = client.call("post", post_id, record) 
        assert result['succeeded'], result 
        #print("OK") 
      else: 
       #print("No record exists for {0}".format(dnsname)) 

    # Sleep at end of loop. 
    time.sleep(60.0) 

의견을 보내 주시면 대단히 감사하겠습니다. Gert

+0

sleep은 while 루프의 일부가되어야하지만 외부에있는 것처럼 보입니다. –

+0

@ Shadow : 나는 파이썬의 빈 코드 블록에 대해이 제한을 알지 못했습니다. 이것이 내가 필요한 정보였습니다. +1 + 답변을 수락했습니다. –

답변

4

문제는 여기에 있습니다.

else: 
    #print("No record exists for {0}".format(dnsname)) 

의견은 들여 쓰기가 고려되는 한 코드로 간주되지 않습니다. 그래서 그 자리에 실제 코드가 있어야합니다.

이 방법은 파이썬의 pass 키워드입니다.

else: 
    #print("No record exists for {0}".format(dnsname)) 
    pass 

이것은 들여 쓰기 수준에서 필요한 코드를 의도적으로 비워 놨다는 것을 파이썬에 알립니다.

또는 코드의 주석을 제거하고이 문제를 해결할 수도 있습니다. 물론 실제로 인쇄하려고한다고 가정하십시오.

+1

FWIW, 리터럴 문자열도 명령문으로 간주되므로''print ("{0}"에 대한 레코드가 없습니다. 형식 (dnsname)) ''와 같이 주석을 달지 말고 줄 주위에 따옴표를 넣습니다. 작업. – martineau

+0

기술적으로 그건 사실입니다. 제 의견으로는 꽤 못 생깁니다 ... – Shadow

+0

삼중 따옴표를 사용할 수도 있기 때문에 여러 줄을 사용할 때 유용합니다. – martineau

1

마지막 else 블록에는 유효한 명령문이 필요합니다. pass을 사용하십시오.

0

파이썬은 귀하의 time.sleep(60.0) 성명 (위의 내용은 귀하의 코멘트 인 "No record exists"print() 호보다 위)에있는 else 절 다음의 성명을 찾고 있습니다. 파이썬이 적절한 들여 쓰기가있는 행을 else 절로 분류하면 오류가 발생합니다.

이 문제를 우회하는 경향이있는 가장 좋은 방법은 None을 참조하거나 pass을 사용하고이 문제를 회피하기 위해 선택적으로 주석을 추가하는 것입니다. 이 도움이

# ... 
     else: 
      # TODO 
      # print("No record exists for {0}".format(dnsname)) 
      pass 

    # Sleep at end of loop. 
    time.sleep(60.0) 

희망 다음 else 절 근처 코드는 다음과 유사 보일 것입니다!