CherryPy WS在UTF8中没有返回字符串

2024-10-03 15:23:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用CherryPy和Python构建restweb服务。它可以工作,但当我通过Chrome访问它时,它不会显示在UTF-8中

此web服务查询MongoDB,然后在字典列表中给出结果。我做了一个打印(ticketME),它显示了正确的字符:

enter image description here

但当它在Chrome中显示时,显示不正确(我也意识到“solucion”或“problema”没有显示……):

enter image description here

正如您在代码中看到的,我将字符集设置为UTF-8:

import cherrypy
import pymongo
import urllib
import pyodbc
import mysql.connector
from datetime import datetime
import time
import sys
import numpy as np

class HelloWorld(object):


    @cherrypy.expose
    @cherrypy.tools.json_out()
    def index(self):
        password = * password *
        myclient = pymongo.MongoClient(*mongoDB connection string*)
        mydb = myclient["moica2"]
        mycol = mydb["moicaTickets"]

        myquery = *mongoDB aggregate query*
        mydoc = mycol.aggregate(myquery)
        mydb = None
        myclient.close()
        mycol = None

        resultadoTicketsME = []

        for x in mydoc:
            try:
                asunto = x['pAsunto']
                nrotkt = x['pTicket']
                estado = x['pEstado']
                fechaCreacion = x['pFechaCreacion']
                fechaCierre = x['pFechaCierre']
                nodoCRM =  x['pNodoCRM']
                nodoCMTS = x['pNodoCMTS']
                if ('COMPLETO' in nodoCMTS):
                    nodoCMTS = "Completo"
                RTs = x['pRTs']
                notas = x['pNotas']
                asuntoCierre = x['pAsuntoCierre']
                estadoEtaClick = x['pEstadoEtaClick']
                afectacion = x['pAfectacion']
                problema = x['pProblema']
                solucion = x['pSolucion']
                arbolCreacion = x['pElArbolCreacion']
                arbolActual = x['pElarbolActual']
                idFuente = int(x['pFuente']['idFuente'])

                ticketME = {
                    'nrotkt': nrotkt, 
                    'asunto': asunto, 
                    'estado': estado, 
                    'fechaCreacion': fechaCreacion, 
                    'fechaCierre': fechaCierre, 
                    'nodoCRM': nodoCRM, 
                    'nodoCMTS': nodoCMTS, 
                    'RTs': RTs, 
                    'notas': notas, 
                    'asuntoCierre': asuntoCierre, 
                    'estadoEtaClick': estadoEtaClick, 
                    'afectacion': afectacion, 
                    'problema': problema, 
                    'solucion': solucion, 
                    'arbolCreacion': arbolCreacion, 
                    'arbolActual': arbolActual, 
                    'idFuente': idFuente
                }

                print(ticketME)
                resultadoTicketsME.append(ticketME)

            except:

                lf = open("error.log", "a+")
                lf.write("MOICA2FUENTESME %s : No se pudo insertar el tkt %s\n" % (datetime.now().strftime("%Y-%m-%d %H:%M:%S"),x['pTicket']))
                lf.close()

        cherrypy.response.headers['Content-Type'] = "text/html;charset=utf-8" 
        return  resultadoTicketsME

USERS = {'ngabioud': 'password'}

def validate_password(realm, username, password):
    if username in USERS and USERS[username] == password:
       return True
    return False

cherrypy.config.update({'tools.encode.on': True,
                         'tools.encode.encoding': 'utf-8',
                         'tools.decode.on': True,
                         'tools.auth_basic.on': True,
                        'tools.auth_basic.realm': 'localhost',
                        'tools.auth_basic.checkpassword': validate_password,
                        'tools.auth_basic.accept_charset': 'UTF-8',
                        })
cherrypy.quickstart(HelloWorld())

还有什么我可以试试的吗

谢谢你, 致以最诚挚的问候


Tags: importauthtruedatetimebasicpasswordtoolsutf
1条回答
网友
1楼 · 发布于 2024-10-03 15:23:40

正如snakecharmerb在评论中所说,这是一个Chrome代表的问题。我做了一个.php设置编码为utf-8,结果显示正确

相关问题 更多 >