def do_POST(self): '''Receive get requests and read post request body''' self._set_headers() content_length = int(self.headers['Content-Length']) post_body = self.rfile.read(content_length) content_type = (chardet.detect(post_body)) print(content_type) dec_body = post_body.decode('latin_1') thread_number = threading.currentThread().getName()[-1] json_file_name = 'data{0}.json'.format(thread_number) with open(json_file_name, "w") as file_j: # Here we create our files depending on thread file_j.write('[') wrong_file_name = 'wrong{0}.txt'.format(thread_number) with open(wrong_file_name, "w") as wrong_f: pass count_right = 0 count_wrong = 0 for i in dec_body.split('\n'): # Here we use our parser for lines and add processed lines to files answer = string_parse(i) if type(answer) == dict: count_right += 1 with open(json_file_name, "a") as file_j: json.dump(answer, file_j) else: count_wrong += 1 with open(wrong_file_name, "a") as wrong_f: wrong_f.write(answer + '\n') with open(json_file_name, "a") as file_j: file_j.write(']') self.wfile.write(b'received post request\nCount of right strings = ' + str(count_right).encode() + b'\n' + b'Count of wrong strings = ' + str(count_wrong).encode() + b'\n') # Return answer
def test_correct_pipes(self): self.assertEqual( string_parse(r'\|\|\|aa|b b b|c|d\| \| |vvvvv|g||'), { 'param2': 'b b b', 'param3': 'c', 'param7': '', 'param6': 'g', 'param1': '|||aa', 'param4': 'd| | ', 'param5': 'vvvvv' }, "Your function is wrong")
def test_only_pipes(self): self.assertEqual( string_parse("|||||||"), { 'param2': '', 'param1': '', 'param4': '', 'param7': '', 'param3': '', 'param6': '', 'param5': '' }, "Your function is wrong")
def test_without_pairs(self): self.assertEqual( string_parse(r'a|b b|cc|d d d|e|f|g|'), { 'param5': 'e', 'param4': 'd d d', 'param2': 'b b', 'param1': 'a', 'param3': 'cc', 'param6': 'f', 'param7': 'g' }, "Something Wrong")
def test_correct_equal_sign(self): self.assertEqual( string_parse( r'a|b b|cc|d d d|e|f|g| key1=value1 key2=valu \= e2 key3=value3 keyN=valueN' ), { 'key2': 'valu = e2 ', 'param4': 'd d d', 'param7': 'g', 'key1': 'value1', 'param2': 'b b', 'param3': 'cc', 'param6': 'f', 'param1': 'a', 'param5': 'e', 'keyN': 'valueN', 'key3': 'value3' }, "Something Wrong")
def some_sort_of_random_data(self, s): self.assertEqual(string_parse(s), s, "Ooopss")
def test_blank(self): self.assertEqual(string_parse(''), '', "Your function is wrong")