Python: вызов другого локального сервера с локального хоста
def jsonCatch(environ,start_response):
results = requests.get("http://localhost:8055/jsonResponse")
start_response('200 OK', [('Content-Type', 'application/json')])
return results.json()
from wsgiref.simple_server import make_server
httpd = make_server('', 8050, application)
print('Serving on port 8050...')
httpd.serve_forever()
обслуживающий порт 8055
def jsonResponse(environ,start_response):
responseData={}
responseData['name']="alex"
responseData['age']="12"
start_response('200 OK',[('Content-Type', 'application/json')])
return json.dumps(responseData)
[ERROR]
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
self.finish_response()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 128, in finish_response
self.write(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 204, in write
assert type(data) is StringType,"write() argument must be string"
AssertionError: write() argument must be string
127.0.0.1 - - [07/Apr/2015 09:50:04] "GET /jsonCatch HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52274)
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/simple_server.py", line 124, in handle
handler.run(self.server.get_app())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 92, in run
self.close()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/simple_server.py", line 33, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
Изменить: изменен https на http и ошибка на сервере
Я пытаюсь получить доступ к серверу локального хоста, который возвращает ответ json и используется другим сервером локального хоста. Может кто-нибудь показать мне пример этого?
2 ответа
Я изменил вышеуказанный код на
n=json.loads(results.text)
start_response('200 OK', [('Content-Type', 'text/plain')])
return n['name'].encode('utf-8')
и это возвращает меня "Алекс"
Попробуйте использовать http://
вместо https://
на этой линии: results = requests.get("https://localhost:8055/jsonResponse")