Search code examples
javapythonurllib2flask-restful

python urllib2. Cannot get data from same IP address of My Server


Here is my code I'm using in python flask to get data from my own server IP.

url = 'http://aa.bb.ccc.ddd:8082/v2/check?'
values = {'language': language,
          'text': text
          }
user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
headers = {'User-Agent': user_agent, 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
output = urllib2.urlopen(req).read()

Details: I have a Java service running on my server on port 8081 which accepts parameter with GET and send some response, here is the URL http://aa.bb.ccc.ddd:8081/v2/check?language=en-US&text=my+text If I send a request to this in the browser it sends me the response in JSON smoothly.

Now based on this result I have to perform some actions in python and send back to the client when they request from my flask service.

For this I have a service in flask python running on port 8084, the URL is http://aa.bb.ccc.ddd:8084/api/askFromAPI

What I want is that when some client sends me to request on flask web service, based on parameters I can request to my own server URL of java service http://aa.bb.ccc.ddd:8081/v2/check?language=en-US&text=my+text, get a response, prepare results from that and send back to the client, but it always timeout.

Client-> FlaskAPI:8084 -> Req. to Java:8081 -> Response to Flask:8084 -> Value Add in Response -> Return to Client


Solution

  • Well after 2 days to hard work I finally contacted my server provider, and he told me that they have blocked the ports on which I was trying to connect, so after port enabling my script successfully fetched the data from the service.