Fix Python3's sending bytes versus strings
Change-Id: Ie2557766ad110c1a3fb4721e902a41aa88cd7272
This commit is contained in:
parent
caa3cb7710
commit
5f009133dd
|
@ -30,7 +30,7 @@ import json
|
|||
import os
|
||||
import sys
|
||||
import yaml
|
||||
if (sys.version_info >= (3, 0)):
|
||||
if sys.version_info >= (3, 0):
|
||||
import http.client # noqa
|
||||
else:
|
||||
import httplib # noqa
|
||||
|
@ -95,15 +95,20 @@ class MoltenIron(object):
|
|||
"""Send the generated request """
|
||||
ip = str(self.conf['serverIP'])
|
||||
port = int(self.conf['mi_port'])
|
||||
if (sys.version_info > (3, 0)):
|
||||
if sys.version_info > (3, 0):
|
||||
connection = http.client.HTTPConnection(ip, port) # noqa
|
||||
else:
|
||||
connection = httplib.HTTPConnection(ip, port) # noqa
|
||||
connection.request('POST', '/', json.dumps(request))
|
||||
|
||||
response = connection.getresponse()
|
||||
data = response.read()
|
||||
|
||||
return response.read()
|
||||
if sys.version_info > (3, 0):
|
||||
# We actually receive bytes instead of a string!
|
||||
data = data.decode("utf-8")
|
||||
|
||||
return data
|
||||
|
||||
def get_response(self):
|
||||
"""Returns the response from the server """
|
||||
|
|
|
@ -104,7 +104,11 @@ def MakeMoltenIronHandlerWithConf(conf):
|
|||
def do_POST(self):
|
||||
"""HTTP POST support"""
|
||||
CL = 'Content-Length'
|
||||
self.data_string = self.rfile.read(int(self.headers[CL]))
|
||||
data = self.rfile.read(int(self.headers[CL]))
|
||||
if sys.version_info >= (3, 0):
|
||||
# We actually received bytes instead of a string!
|
||||
data = data.decode("utf-8")
|
||||
self.data_string = data
|
||||
response = self.parse(self.data_string)
|
||||
self.send_reply(response)
|
||||
|
||||
|
@ -117,8 +121,11 @@ def MakeMoltenIronHandlerWithConf(conf):
|
|||
self.send_response(status_code)
|
||||
self.send_header('Content-type', 'application/json')
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps(response,
|
||||
cls=JSON_encoder_with_DateTime))
|
||||
data = json.dumps(response, cls=JSON_encoder_with_DateTime)
|
||||
if sys.version_info >= (3, 0):
|
||||
# We actually need to send bytes instead of a string!
|
||||
data = data.encode()
|
||||
self.wfile.write(data)
|
||||
|
||||
def parse(self, request_string):
|
||||
"""Handle the request. Returns the response of the request """
|
||||
|
|
Loading…
Reference in New Issue