当POSTed数据中有双引号时,为什么python urllib.request.urlopen()会失败?

时间:2016-04-15 20:10:24

标签: python json post urllib

我正在尝试使用urllib.request.urlopen()进行POST以及我的工作完美,除非数据中有双引号。我在调用urlopen()之前使用urllib.parse.urlencode()和bytes()。我还需要做什么?这是功能:

def getHypotheses(server, lang, dym, query):
    param = urllib.parse.urlencode({"query": query, "count": "10"})
    param_data = bytes(param,"utf-8")
    url = "%s/aqs/%s/%s" % (server, lang, dym)
    print ('query = ', query)
    try:
        connection = urllib.request.urlopen(url, param_data)
    except urllib.error.HTTPError as err:
        print ('param = ', param)
        print ('url = ', url, ', query = ', query, ',
                param_data = ', param_data)
        sys.exit()
    response = connection.read().decode('utf-8')
    jsonobj = json.loads(response)
    return jsonobj

和输出样本:

query =  IHBK
query =  B;NS
query =  ASI
query =  MDN
query =  "CMANI
param =  count=10&query=%22CMANI
url =  http://localhost:8080/myserver/aqs/foo/bar ,
       query =  "CMANI , param_data =  b'count=10&query=%22CMANI'

有什么想法吗?谢谢! - 戴夫

0 个答案:

没有答案