cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
create_post_role.py
Go to the documentation of this file.
1 # *
2 # * Copyright 2016 -- 2022 IBM Corporation
3 # *
4 # * Licensed under the Apache License, Version 2.0 (the "License");
5 # * you may not use this file except in compliance with the License.
6 # * You may obtain a copy of the License at
7 # *
8 # * http://www.apache.org/licenses/LICENSE-2.0
9 # *
10 # * Unless required by applicable law or agreed to in writing, software
11 # * distributed under the License is distributed on an "AS IS" BASIS,
12 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # * See the License for the specific language governing permissions and
14 # * limitations under the License.
15 # *
16 
17 import sys
18 
19 __switch_payload__ = False
20 
21 if __name__ == '__main__':
22 
23  with open(sys.argv[1], 'rb') as in_file:
24  pr_file = bytearray(in_file.read())
25 
26  #requestString = "POST /configure HTTP/1.1\r\nUser-Agent: curl/7.29.0\r\nHost: localhost:8080\r\nAccept: */*\r\nContent-Length: "+str(data_len)+"\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n"
27  requestString = "POST /configure HTTP/1.1\r\nUser-Agent: curl/7.29.0\r\nContent-Type: application/x-www-form-urlencodedAB\r\n\r\n"
28 
29  pr_len = len(pr_file)
30  pr_words = int(pr_len/4)
31  assert(pr_len %4 == 0)
32  print("loaded pr bin file of size {} bytes ({} words)...".format(hex(pr_len), hex(pr_words)))
33  byteData = bytearray(requestString.encode())
34  if __switch_payload__:
35  pr_file_size = len(pr_file)
36  assert((pr_file_size % 4) == 0)
37  number_of_words = int(pr_file_size/4)
38  for i in range(0, number_of_words):
39  byteData.append(pr_file[i*4+3])
40  byteData.append(pr_file[i*4+2])
41  byteData.append(pr_file[i*4+1])
42  byteData.append(pr_file[i*4+0])
43  else:
44  byteData.extend(pr_file)
45 
46  byteData.extend(bytearray("\r\n\r\n".encode()))
47 
48  #out_string = byteData.decode('iso-8859-1')
49  #out_string = ''.join(format(x, '02x') for x in byteData)
50  #print(out_string, end='', flush=True)
51 
52  # TODO: does this destroy encoding?
53  #sys.stdout.buffer.write(byteData)
54  #with io.open(sys.argv[2], 'wb', newline='\r\n') as outfile:
55  with open(sys.argv[2], 'wb') as outfile:
56  outfile.write(byteData)
57 
58  print("POST request exported to: {}".format(sys.argv[2]))
59