|
11 | 11 | concore.delay = 0.02 |
12 | 12 |
|
13 | 13 | try: |
14 | | - apikey=open(concore.inpath+'1/concore.apikey',newline=None).readline().rstrip() |
| 14 | + with open(concore.inpath+'1/concore.apikey',newline=None) as f: |
| 15 | + apikey=f.readline().rstrip() |
15 | 16 | except (OSError, IOError): |
16 | 17 | try: |
17 | 18 | #perhaps this should be removed for security |
18 | | - apikey=open('./concore.apikey',newline=None).readline().rstrip() |
| 19 | + with open('./concore.apikey',newline=None) as f: |
| 20 | + apikey=f.readline().rstrip() |
19 | 21 | except (OSError, IOError): |
20 | 22 | apikey = '' |
21 | 23 |
|
22 | 24 | try: |
23 | | - yuyu=open(concore.inpath+'1/concore.yuyu',newline=None).readline().rstrip() |
| 25 | + with open(concore.inpath+'1/concore.yuyu',newline=None) as f: |
| 26 | + yuyu=f.readline().rstrip() |
24 | 27 | except (OSError, IOError): |
25 | 28 | try: |
26 | | - yuyu=open('./concore.yuyu',newline=None).readline().rstrip() |
| 29 | + with open('./concore.yuyu',newline=None) as f: |
| 30 | + yuyu=f.readline().rstrip() |
27 | 31 | except (OSError, IOError): |
28 | 32 | yuyu = 'yuyu' |
29 | 33 |
|
30 | 34 | try: |
31 | | - name1=open(concore.inpath+'1/concore.name1',newline=None).readline().rstrip() |
| 35 | + with open(concore.inpath+'1/concore.name1',newline=None) as f: |
| 36 | + name1=f.readline().rstrip() |
32 | 37 | except (OSError, IOError): |
33 | 38 | try: |
34 | | - name1=open('./concore.name1',newline=None).readline().rstrip() |
| 39 | + with open('./concore.name1',newline=None) as f: |
| 40 | + name1=f.readline().rstrip() |
35 | 41 | except (OSError, IOError): |
36 | 42 | name1 = 'u' |
37 | 43 |
|
38 | 44 | try: |
39 | | - name2=open(concore.inpath+'1/concore.name2',newline=None).readline().rstrip() |
| 45 | + with open(concore.inpath+'1/concore.name2',newline=None) as f: |
| 46 | + name2=f.readline().rstrip() |
40 | 47 | except (OSError, IOError): |
41 | 48 | try: |
42 | | - name2=open('./concore.name2',newline=None).readline().rstrip() |
| 49 | + with open('./concore.name2',newline=None) as f: |
| 50 | + name2=f.readline().rstrip() |
43 | 51 | except (OSError, IOError): |
44 | 52 | name2 = 'ym' |
45 | 53 |
|
46 | 54 | try: |
47 | | - init_simtime_u = open(concore.inpath+'1/concore.init1',newline=None).readline().rstrip() |
| 55 | + with open(concore.inpath+'1/concore.init1',newline=None) as f: |
| 56 | + init_simtime_u = f.readline().rstrip() |
48 | 57 | except (OSError, IOError): |
49 | 58 | try: |
50 | | - init_simtime_u = open('./concore.init1',newline=None).readline().rstrip() |
| 59 | + with open('./concore.init1',newline=None) as f: |
| 60 | + init_simtime_u = f.readline().rstrip() |
51 | 61 | except (OSError, IOError): |
52 | 62 | init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]" |
53 | 63 |
|
54 | 64 | try: |
55 | | - init_simtime_ym = open(concore.inpath+'1/concore.init2',newline=None).readline().rstrip() |
| 65 | + with open(concore.inpath+'1/concore.init2',newline=None) as f: |
| 66 | + init_simtime_ym = f.readline().rstrip() |
56 | 67 | except (OSError, IOError): |
57 | 68 | try: |
58 | | - init_simtime_ym = open('./concore.init2',newline=None).readline().rstrip() |
| 69 | + with open('./concore.init2',newline=None) as f: |
| 70 | + init_simtime_ym = f.readline().rstrip() |
59 | 71 | except (OSError, IOError): |
60 | 72 | init_simtime_ym = "[0.0, 0.0, 0.0]" |
61 | 73 |
|
|
82 | 94 | logging.debug("CW outer loop") |
83 | 95 | while concore.unchanged(): |
84 | 96 | u = concore.read(1,name1,init_simtime_u) |
85 | | - f = {'file1': open(concore.inpath+'1/'+name1, 'rb')} |
86 | | - logging.debug(f"CW: before post u={u}") |
87 | | - logging.debug(f'http://www.controlcore.org/pm/{yuyu}{apikey}&fetch={name2}') |
88 | | - r = requests.post('http://www.controlcore.org/pm/'+yuyu+apikey+'&fetch='+name2, files=f,timeout=timeout_max) |
| 97 | + with open(concore.inpath+'1/'+name1, 'rb') as f1: |
| 98 | + f = {'file1': f1} |
| 99 | + logging.debug(f"CW: before post u={u}") |
| 100 | + logging.debug(f'http://www.controlcore.org/pm/{yuyu}{apikey}&fetch={name2}') |
| 101 | + r = requests.post('http://www.controlcore.org/pm/'+yuyu+apikey+'&fetch='+name2, files=f,timeout=timeout_max) |
89 | 102 | if r.status_code!=200: |
90 | 103 | logging.error(f"bad POST request {r.status_code}") |
91 | 104 | quit() |
|
101 | 114 | while oldt==t or len(r.content)==0: |
102 | 115 | time.sleep(concore.delay) |
103 | 116 | logging.debug(f"CW waiting status={r.status_code} content={r.content.decode('utf-8')} t={t}") |
104 | | - f = {'file1': open(concore.inpath+'1/'+name1, 'rb')} |
105 | | - try: |
106 | | - r = requests.post('http://www.controlcore.org/pm/'+yuyu+apikey+'&fetch='+name2, files=f,timeout=timeout_max) |
107 | | - except Exception: |
108 | | - logging.error("CW: bad request") |
| 117 | + with open(concore.inpath+'1/'+name1, 'rb') as f1: |
| 118 | + f = {'file1': f1} |
| 119 | + try: |
| 120 | + r = requests.post('http://www.controlcore.org/pm/'+yuyu+apikey+'&fetch='+name2, files=f,timeout=timeout_max) |
| 121 | + except Exception: |
| 122 | + logging.error("CW: bad request") |
109 | 123 | timeout_count += 1 |
110 | 124 | if r.status_code!=200 or time.perf_counter()-t1 > 1.1*timeout_max: #timeout_count>100: |
111 | 125 | logging.error(f"timeout or bad POST request {r.status_code}") |
|
0 commit comments