fix a bug of Python3 Chinese encoding

This commit is contained in:
suxunbin 2019-07-04 17:10:38 +08:00
parent 57555313a8
commit 4ae5aa4326
1 changed files with 53 additions and 47 deletions

View File

@ -5,8 +5,13 @@
# Description: http api for python
"""
import sys
import requests
version = sys.version[0]
if version == '3':
from urllib import parse
defaultServerIP = "127.0.0.1"
defaultServerPort = "9000"
@ -23,41 +28,42 @@ class GstoreConnector:
def UrlEncode(self, s):
ret = ""
for i in range(len(s)):
c = s[i]
if ((ord(c)==42) or (ord(c)==45) or (ord(c)==46) or (ord(c)==47) or (ord(c)==58) or (ord(c)==95)):
ret += c
elif ((ord(c)>=48) and (ord(c)<=57)):
ret += c
elif ((ord(c)>=65) and (ord(c)<=90)):
ret += c
elif ((ord(c)>=97) and (ord(c)<=122)):
ret += c
elif (ord(c)==32):
ret += '+'
elif (ord(c)>=256):
ret += chr(ord(c))
elif ((ord(c)!=10) and (ord(c)!=11) and (ord(c)!=13)):
ret += "{}{:X}".format("%", ord(c))
if version == '2':
for i in range(len(s)):
c = s[i]
if ((ord(c)==42) or (ord(c)==45) or (ord(c)==46) or (ord(c)==47) or (ord(c)==58) or (ord(c)==95)):
ret += c
elif ((ord(c)>=48) and (ord(c)<=57)):
ret += c
elif ((ord(c)>=65) and (ord(c)<=90)):
ret += c
elif ((ord(c)>=97) and (ord(c)<=122)):
ret += c
elif (ord(c)==32):
ret += '+'
elif ((ord(c)!=9) and (ord(c)!=10) and (ord(c)!=13)):
ret += "{}{:X}".format("%", ord(c))
elif version == '3':
ret = parse.quote(s)
return ret
def Get(self, strUrl):
r = requests.get(self.UrlEncode(strUrl))
r = requests.get(self.Url + self.UrlEncode(strUrl))
return r.text
def Post(self, strUrl, strPost):
r = requests.post(self.UrlEncode(strUrl), strPost)
r = requests.post(self.Url + self.UrlEncode(strUrl), strPost)
return r.text
def fGet(self, strUrl, filename):
r = requests.get(self.UrlEncode(strUrl), stream=True)
r = requests.get(self.Url + self.UrlEncode(strUrl), stream=True)
with open(filename, 'wb') as fd:
for chunk in r.iter_content(4096):
fd.write(chunk)
return
def fPost(self, strUrl, strPost, filename):
r = requests.post(self.UrlEncode(strUrl), strPost, stream=True)
r = requests.post(self.Url + self.UrlEncode(strUrl), strPost, stream=True)
with open(filename, 'wb') as fd:
for chunk in r.iter_content(4096):
fd.write(chunk)
@ -65,70 +71,70 @@ class GstoreConnector:
def build(self, db_name, rdf_file_path, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=build&db_name=" + db_name + "&ds_path=" + rdf_file_path + "&username=" + self.username + "&password=" + self.password
strUrl = "/?operation=build&db_name=" + db_name + "&ds_path=" + rdf_file_path + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/build"
strUrl = "/build"
strPost = '{\"db_name\": \"' + db_name + '\", \"ds_path\": \"' + rdf_file_path + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def load(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=load&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
strUrl = "/?operation=load&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/load"
strUrl = "/load"
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def unload(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=unload&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
strUrl = "/?operation=unload&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/unload"
strUrl = "/unload"
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def user(self, type, username2, addition, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=user&type=" + type + "&username1=" + self.username + "&password1=" + self.password + "&username2=" + username2 + "&addition=" +addition
strUrl = "/?operation=user&type=" + type + "&username1=" + self.username + "&password1=" + self.password + "&username2=" + username2 + "&addition=" +addition
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/user"
strUrl = "/user"
strPost = '{\"type\": \"' + type + '\", \"username1\": \"' + self.username + '\", \"password1\": \"' + self.password + '\", \"username2\": \"' + username2 + '\", \"addition\": \"' + addition + '\"}'
res = self.Post(strUrl, strPost)
return res
def showUser(self, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=showUser&username=" + self.username + "&password=" + self.password
strUrl = "/?operation=showUser&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/showUser"
strUrl = "/showUser"
strPost = '{\"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def query(self, db_name, format, sparql, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=query&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql
strUrl = "/?operation=query&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/query"
strUrl = "/query"
strPost = '{\"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\", \"db_name\": \"' + db_name + '\", \"format\": \"' + format + '\", \"sparql\": \"' + sparql + '\"}'
res = self.Post(strUrl, strPost)
return res
def fquery(self, db_name, format, sparql, filename, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=query&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql
strUrl = "/?operation=query&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql
self.fGet(strUrl, filename)
elif request_type == 'POST':
strUrl = self.Url + "/query"
strUrl = "/query"
strPost = '{\"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\", \"db_name\": \"' + db_name + '\", \"format\": \"' + format + '\", \"sparql\": \"' + sparql + '\"}'
self.fPost(strUrl, strPost, filename)
return
@ -136,12 +142,12 @@ class GstoreConnector:
def drop(self, db_name, is_backup, request_type='GET'):
if request_type == 'GET':
if is_backup:
strUrl = self.Url + "/?operation=drop&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password + "&is_backup=true"
strUrl = "/?operation=drop&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password + "&is_backup=true"
else:
strUrl = self.Url + "/?operation=drop&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password + "&is_backup=false"
strUrl = "/?operation=drop&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password + "&is_backup=false"
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/drop"
strUrl = "/drop"
if is_backup:
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\", \"is_backup\": \"true\"}'
else:
@ -151,50 +157,50 @@ class GstoreConnector:
def monitor(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=monitor&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
strUrl = "/?operation=monitor&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/monitor"
strUrl = "/monitor"
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def checkpoint(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=checkpoint&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
strUrl = "/?operation=checkpoint&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/checkpoint"
strUrl = "/checkpoint"
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def show(self, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=show&username=" + self.username + "&password=" + self.password
strUrl = "/?operation=show&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/show"
strUrl = "/show"
strPost = '{\"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def getCoreVersion(self, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=getCoreVersion&username=" + self.username + "&password=" + self.password
strUrl = "/?operation=getCoreVersion&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/getCoreVersion"
strUrl = "/getCoreVersion"
strPost = '{\"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def getAPIVersion(self, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=getAPIVersion&username=" + self.username + "&password=" + self.password
strUrl = "/?operation=getAPIVersion&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/getAPIVersion"
strUrl = "/getAPIVersion"
strPost = '{\"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res