简体   繁体   English

Python:从本地主机调用另一个本地主机服务器

[英]Python : Calling another localhost server from localhost

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()

serving on port 8055 在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'

Edit: Changed https to http and error on server 编辑:将https更改为http并在服务器上出错

I am trying to access a localhost server which returns a json response and is used by another localhost server. 我正在尝试访问一个本地服务器,该服务器返回一个json响应,并由另一个本地服务器使用。 Can somebody show me an example of it? 有人可以给我举个例子吗?

I have changed the above code to 我已将上面的代码更改为

n=json.loads(results.text)
start_response('200 OK', [('Content-Type', 'text/plain')])
return n['name'].encode('utf-8')

and it returns me "alex" 它返回我“ alex”

尝试在此行上使用http://而不是https://results = requests.get("https://localhost:8055/jsonResponse")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM