python jsonrpc错误错误网关错误
我试图理解python中的JSON rpcpython jsonrpc错误错误网关错误,python,json,http,rpc,json-rpc,Python,Json,Http,Rpc,Json Rpc,我试图理解python中的JSON rpc #!/usr/bin/env python # coding: utf-8 import pyjsonrpc def add(a, b): """Test function""" return a + b class RequestHandler(pyjsonrpc.HttpRequestHandler): # Register public JSON-RPC methods
#!/usr/bin/env python
# coding: utf-8
import pyjsonrpc
def add(a, b):
"""Test function"""
return a + b
class RequestHandler(pyjsonrpc.HttpRequestHandler):
# Register public JSON-RPC methods
methods = {
"add": add
}
# Threading HTTP-Server
http_server = pyjsonrpc.ThreadingHttpServer(
server_address = ('localhost', 8080),
RequestHandlerClass = RequestHandler
)
print "Starting HTTP server ..."
print "URL: http://localhost:8080"
http_server.serve_forever()
#!/usr/bin/env python
# coding: utf-8
import pyjsonrpc
http_client = pyjsonrpc.HttpClient(
url = "http://example.com/jsonrpc",
username = "Username",
password = "Password"
)
print http_client.call("add", 1, 2)
# Result: 3
# It is also possible to use the *method* name as *attribute* name.
print http_client.add(1, 2)
15 # Result: 3
我的客户如下所示
#!/usr/bin/env python
# coding: utf-8
import pyjsonrpc
def add(a, b):
"""Test function"""
return a + b
class RequestHandler(pyjsonrpc.HttpRequestHandler):
# Register public JSON-RPC methods
methods = {
"add": add
}
# Threading HTTP-Server
http_server = pyjsonrpc.ThreadingHttpServer(
server_address = ('localhost', 8080),
RequestHandlerClass = RequestHandler
)
print "Starting HTTP server ..."
print "URL: http://localhost:8080"
http_server.serve_forever()
#!/usr/bin/env python
# coding: utf-8
import pyjsonrpc
http_client = pyjsonrpc.HttpClient(
url = "http://example.com/jsonrpc",
username = "Username",
password = "Password"
)
print http_client.call("add", 1, 2)
# Result: 3
# It is also possible to use the *method* name as *attribute* name.
print http_client.add(1, 2)
15 # Result: 3
我按如下方式启动http服务器
“$python http_server.py”
Starting HTTP server ...
URL: http://localhost:8080
然后我启动客户机,如下所示
$python http_client.py
我得到以下错误
Traceback (most recent call last):
File "http_client.py", line 11, in <module>
print http_client.call("add", 1, 2)
File "/usr/local/lib/python2.7/dist-packages/pyjsonrpc/http.py", line 98, in call
password = self.password
File "/usr/local/lib/python2.7/dist-packages/pyjsonrpc/http.py", line 33, in http_request
response = urllib2.urlopen(request)
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 406, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 444, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 502: Bad Gateway
回溯(最近一次呼叫最后一次):
文件“http_client.py”,第11行,在
打印http_客户端调用(“添加”,1,2)
文件“/usr/local/lib/python2.7/dist packages/pyjsonrpc/http.py”,第98行,在调用中
password=self.password
http_请求中的文件“/usr/local/lib/python2.7/dist packages/pyjsonrpc/http.py”,第33行
response=urllib2.urlopen(请求)
文件“/usr/lib/python2.7/urllib2.py”,第126行,在urlopen中
return\u opener.open(url、数据、超时)
文件“/usr/lib/python2.7/urllib2.py”,第406行,打开
响应=方法(请求,响应)
http_响应中的文件“/usr/lib/python2.7/urllib2.py”,第519行
“http”、请求、响应、代码、消息、hdrs)
文件“/usr/lib/python2.7/urllib2.py”,第444行出错
返回自我。调用链(*args)
文件“/usr/lib/python2.7/urllib2.py”,第378行,在调用链中
结果=func(*args)
文件“/usr/lib/python2.7/urllib2.py”,第527行,默认为http\u error\u
raise HTTPError(请求获取完整url(),代码,消息,hdrs,fp)
urllib2.HTTPError:HTTP错误502:错误网关
在客户端脚本中,将URL更改为localhost:8080以匹配服务器设置。此外,对于这个简单的实现,您不需要用户名或密码。我在下面提供了一个客户端的工作版本
#!/usr/bin/env python
# coding: utf-8
import pyjsonrpc
http_client = pyjsonrpc.HttpClient(
url = "http://localhost:8080"
)
print http_client.call("add", 1, 2)
# Result: 3
# It is also possible to use the *method* name as *attribute* name.
print http_client.add(1, 2)
# Result: 3