スマホアプリで、クライアントの端末からサーバにリクエストを投げて、その返答を返す単純なことをやっています。
クライアント側:iOS, Swift 2
サーバ側:CentOS7, Python3.5.1, postgresql9.5, apache2.4.x, mod_wsgi
【やりたいこと】
1)クライアント側でメールアドレスをリクエストとしてサーバ側に投げる
2)サーバ側で受け取ったメールアドレスをPython + mod_wsgiで受け取る
3)受け取ったデータをpostgresqlのSQLの引数として問い合わせ
4)SQLで取得したデータをJSONに変換してクライアントに返す
5)クライアント側で受け取ったデータを表示する
【困ったこと】
postgresqlで取得したデータの中に日本語のデータがあります。
Pythonでコンソール出力してみると、unicode化された状態で出力されました。
Apache error.log
# tail /usr/local/apache2/logs/error_log [Sun May 22 09:00:28.749747 2016] [wsgi:error] [pid 9554:tid 139810162280192] [{'contents': '\xe3\x81\x93\xe3\x82\x8c\xe3\x81\xaf\xe6\x9c\xac\xe6\x96\x87\xe3\x81\xa7\xe3\x81\x99', 'booktime': '2016/05/17 10:00:00', 'subject': '\xe3\x81\x93\xe3\x82\x8c\xe3\x81\xaf\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88\xe3\x81\xa7\xe3\x81\x99', 'mailno': '1 ', 'sendtime': None}] [Sun May 22 09:00:28.749990 2016] [wsgi:error] [pid 9554:tid 139810162280192] [{"contents": "\\u3053\\u308c\\u306f\\u672c\\u6587\\u3067\\u3059", "booktime": "2016/05/17 10:00:00", "subject": "\\u3053\\u308c\\u306f\\u30c6\\u30b9\\u30c8\\u3067\\u3059", "mailno": "1 ", "sendtime": null}] [Sun May 22 09:00:28.750189 2016] [wsgi:error] [pid 9554:tid 139810162280192] [client 192.168.3.2:51081] mod_wsgi (pid=9554): Exception occurred processing WSGI script '/opt/web/remindermail/getMail.py'. [Sun May 22 09:00:28.750357 2016] [wsgi:error] [pid 9554:tid 139810162280192] [client 192.168.3.2:51081] TypeError: sequence of byte string values expected, value of type str found (pyenv3.5) [root@testdb site-packages]#
Python /opt/web/remindermail/getMail.py
python
1# -*- coding: utf-8 -*- 2import db.connectionConf as driver 3import psycopg2 4import psycopg2.extras 5import urllib 6import cgi 7import sys 8import io 9import umsgpack 10import json 11import traceback 12 13def application(env, start_response): 14 #sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') 15 16 if env.get('REQUEST_METHOD') != 'POST': 17 print('Invalid Method') 18 return applicationResponse('403 Forbidden', '', start_response) 19 20 inputData = env.get('wsgi.input') 21 length = int(env.get('CONTENT_LENGTH', 0)) 22 query = urllib.parse.parse_qs(inputData.read(length)) 23 accountname = query[b'accountname'][0].decode('utf-8') 24 25 sql = "select mailno,to_char(booktime,'YYYY/MM/DD HH24:MI:SS'),subject,contents,to_char(sendtime,'YYYY/MM/DD HH24:MI:SS') from rm_mail where accountname=%s" 26 cur = None 27 conn = None 28 29 try: 30 conn = psycopg2.connect( 31 host = driver.HOST, 32 port = driver.PORT, 33 database = driver.DATABASE, 34 user = driver.USER, 35 password = driver.PASSWORD) 36 37 cur = conn.cursor(cursor_factory = psycopg2.extras.DictCursor) 38 cur.execute(sql, (accountname, )) 39 result = [] 40 for row in cur: 41 rowdata = {"mailno": row[0], "booktime": row[1], "subject": row[2], "contents": row[3], "sendtime": row[4] } 42 result.append(rowdata) 43 44 print(result) 45 packdata = json.dumps(result) 46 print(packdata) 47 return applicationResponse('200 OK', packdata, start_response) 48 except: 49 traceback.print_exc() 50 return applicationResponse('500 Internal Server Error', '', start_response) 51 finally: 52 cur.close() 53 conn.close() 54 55 56def applicationResponse(status, data, start_response): 57 response_headers = [('Content-type', 'text/json; charset=utf-8'),('Content-Length', str(len(data)))] 58 start_response(status, response_headers) 59 return data
参考までにクライアント側ソースコード
swift
1// 中略 2 override func viewDidLoad() { 3 super.viewDidLoad() 4 // Do any additional setup after loading the view, typically from a nib. 5 6 let config:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration() 7 let session:NSURLSession = NSURLSession(configuration:config, delegate:nil, delegateQueue:NSOperationQueue.mainQueue()) 8 let url:NSURL = NSURL(string:"http://*****.com/remindermail/getMail/")! 9 let request:NSMutableURLRequest = NSMutableURLRequest(URL:url) 10 request.HTTPMethod = "POST" 11 12 let str:NSString = "accountname=****@gmail.com" 13 request.HTTPBody = str.dataUsingEncoding(NSUTF8StringEncoding)! 14 15 let task:NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: { 16 (data, response, error) -> Void in 17 if let _ = error { 18 print("Request failed with error") 19 print(error) 20 } else { 21 let result = String(data:data!, encoding: NSUTF8StringEncoding) 22 print("Request succeeded with data ¥(_)") 23 print(result) //レスポンスされたJSONデータが出力されれば成功 24 } 25 }) 26 task.resume() 27 } 28// 中略
Python側でJSONデータを返す際に、"TypeError: sequence of byte string values expected, value of type str found"というエラーが発生し、おそらく日本語を含めたデータを返すと、こうなるのかなという認識です。
上記のPythonのソースコードでは、subject, contentsに当たる値が日本語であり、これをprintで出力したのがerror.logの1行目、2行目です。
【解決したいこと】
・postgresqlで取得した日本語データをprintで出力の際に、
日本語として出力させたい
・"TypeError: sequence of byte string values expected, value of type str found"を解決し、
日本語を含めたJSONデータを返したい。
どうすればいいのでしょうか?
ご教授お願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。