Yeah. This is the code.
First I am running app A and B. App B uses flask libraries that is why I had to put it under wsgicontainer.
from tornado.ioloop import IOLoop
import app_A
import flask_app_B
def shutdown_hook():
IOLoop.instance().stop()
app_B = WSGIContainer(flask_app_B)
server_app = Application([(r"/random_dir", app_A),(r".*", FallbackHandler, dict(fallback=app_B))])
http_server = HTTPServer(server_app)
http_server.listen(9090)
IOLoop.instance().start()
Now inside my app_A, I have a method defined which calls a REST API served by app_B. My original purpose is to share some data between these two apps. I dont want go for database options as it will be an overkill solution. So I was hoping that anyway app_B has REST apis which could be called from inside app_A using httpclient libraries(or one you mentioned).
in that case typical work flow is
USER calls method1 inside app_A ----> method1 internally uses httpclient/adynchttpclient libraries to call REST api served by app_B --> Rest api from app_B gives reply to app_A's method1 ---> app_A's method1 replied back to user.
This is what i am doing inside when user calls method1 of app_A.
import httplib, time
import urlparse, tornado.httpclient
url = "http://localhost:9090/some_rest_api"
headers = {'some_data': 'my_data'}
req = tornado.httpclient.HTTPRequest(url, method='GET', headers=headers)
http_client = tornado.httpclient.HTTPClient()
try:
response = http_client.fetch(req)
print response.body
except tornado.httpclient.HTTPError as e:
# HTTPError is raised for non-200 responses; the response
# can be found in e.response.
print("Error1: " + str(e))
except Exception as e:
# Other errors are possible, such as IOError.
print("Error2: " + str(e))
http_client.close()Above code seems to work for any url apart from one served on this server. If I start some example server on other port and ask method1 to call that it is working.
In app_B(flask app) I have following rest_api defined to which i am calling.
@app_B.route('/some_rest_api')
def some_rest_api():
return 'true'I tried running httpclient code separately, which works. It calls this some_rest_api service and gets the response.
But not from within the app_A.
Anyway I am going to try run these two apps under separate ports on the same tornado instance.