特定のポートからデータを取得する Python スクリプトを作成しています。そのため、ポートが開いている限り、データを取得する while ループが発生しました。この while ループでは、変数を追加していますfoo1
。時間切れになったら、それ以上データを取得したくありません。
したがって、擬似コードは次のようになります。
foo1 = 0
try:
while True:
fetch data
foo1 = foo1 + 500
if time up:
break
finally:
close socket
print foo1
私のwhileループの中でfoo1
正しく加算されます。しかし、ループの外側
foo1
は常にゼロです。何か考えはありますか?
foo1をcoh0 editと交換するだけ です:
import re
coh = [0]
nachricht = ' S="0" '
coh0 = 0
time = 0
try:
while True:
time += 1
coh = re.findall(r'\bS="\d"', nachricht)
coh_value = re.findall(r'\d', coh[0])
if coh:
if int(coh_value[0]) == 0:
coh0 = int(coh0) + 500
print coh0
if time == 10:
coh0 = int((int(coh0)/500)/120)
print "Here coh0 is zero again",int(coh0)
break
finally:
pass
print "Here coh0 is zero again",int(coh0)