How to make multiple requests on flask server in Python? - python

I've set up two flask servers on different ports (5000 & 5001). I'm sending a Http request from server 1 and getting a response from server 2. But when I make another request (this time from server 2), I'm getting the below error:
requests.exceptions.ConnectionError:HTTPConnectionPool(host='localhost', port=5000):
Max retries exceeded with url: /fp (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001B072055B50>:
Failed to establish a new connection: [WinError 10061] No connection could be made
because the target machine actively refused it'))
Below is my code for server 1 where I route the request I'm getting to send_hashkeys() function which returns a JSON to server 2. Next, I make a request to get a JSON file from server 2. That's when the above exception is thrown.
#app.route('/')
def send_hashkeys():
return jsonify(hash_list_dict)
#app.route('/s')
def get_file_path():
path_list = requests.get('http://localhost:5000/fp')
Code for server 2 :
message = requests.get('http://localhost:5001/')
message = json.loads(message.text)
#app.route('/fp')
def file_path():
return jsonify(file_path_dict)
This is my first time using flask. Please point out if I'm doing something wrong.

Related

Requests - Python - Failed to establish a new connection: [Errno 111] Connection refused

I have a server running in Ubuntu OS, and I am using Python for getting to learn.
I started an app which I installed, When I opened the app in browser the page is not secure, like this -
I am getting some data from this page using python -
from urllib3.exceptions import InsecureRequestWarning
import requests
requests.urllib3.disable_warnings(category=InsecureRequestWarning)
req = requests.get(url='https://127.0.0.1:7001', verify=False)
This shows an error -
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='127.0.0.1', port=7001): Max retries exceeded with url: / (Caused by
NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fc2ae5f94f0>: Failed to establish a new connection: [Errno 111] Connection refused'))
When I print the variable req to check the status, but python shows the variable is not defined
How to solve this, thanks
HTTPSConnectionPool(host='127.0.0.1', port=443)
443 is the default port for HTTPS.
You're probably (we can't see how you've defined url, but that's my best guess) trying https://127.0.0.1/..., not https://127.0.0.1:7001/... – fix that in your code.

How could I deploy a tabpy code when I already have an API endpoint running using django in an virtual environment?

I am trying to create an api endpoint using django in python and restrserve in R and deploying the below code in tabpy to connect with tableau and I am facing with this error.Could someone recommend on any solution.
Error message:
ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=9004): Max
retries exceeded with url: /endpoints (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x0000016136C007C8>: Failed to establish a new connection: [WinError
10061] No connection could be made because the target machine actively
refused it'))
Code:
import tabpy_client
import requests
client = tabpy_client.Client('http://127.0.0.1:9004')
def external_services_say_hello(message, service):
if service == 'PYTHON':
r = requests.post('http://127.0.0.1:8000/', data ={'message': message})
elif service == 'R':
r = requests.post('http://localhost:8080/hello', data = {'message': message,})
serviceResult = r.json()
return serviceResult['result']
client.deploy('external_services_say_hello', external_services_say_hello, 'Make Python or R say Hello or anything you want.', override = True)
print('*** Model deployed successfully ***')
Thanks in advance!

Connection refused Django requests-module

i'm trying to send GET request from my Django app to Spring app hosted in my local machine.
I've tested sending requests to another websites outside localhost and it works perfectly. Problem appears when it comes to sending to http://localhost:port.
This is function which is sending requests in my Django app. It works for url1 but doesnt for url2.
def send_mail(request):
url1 = "https://httpbin.org/get"
url2 = "http://localhost:5000"
response = requests.get(url2)
return HttpResponse(response)
Here's an error that shows wherever i try send request to localhost.
HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))

how to go to a specific path of host using Python Requests?

I have this host: http://retsau.torontomls.net:7890/and I want to access http://retsau.torontomls.net:7890/rets-treb3pv/server/login, how can I accomplish this using Python Requests? All my attempts till now have failed.
I also followed the solution here - Python Requests - Use navigate site by servers IP and came up with this -
response = requests.get(http://206.152.41.279/rets-treb3pv/server/login, headers={'Host': retsau.torontomls.net})
but that resulted in this error:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='206.152.41.279', port=80): Max retries exceeded with url: /rets-treb3pv/server/login (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x10a4f6d84>: Failed to establish a new connection: [Errno 60] Operation timed out',))
Funny thing is everything seems to work perfectly fine on Postman, I am able to access all sorts of URLs on that server, from logging in to searching for something.
You left out the port number (7890) from the URL to your get call:
response = requests.get('http://206.152.41.279:7890/rets-treb3pv/server/login', headers={'Host': 'retsau.torontomls.net'})
# ^^^^ Add this
Also, unless you actually have a specific reason for accessing the site by IP address, it would make more sense to put the FQDN in the URL rather than the Host header:
response = requests.get('http://retsau.torontomls.net:7890/rets-treb3pv/server/login')

Python soap client - connection having issue

I am using Python soap API client Zeep and here is the code that I have written:
from zeep import Client
def myapi(request):
client = Client("https://siteURL.asmx?wsdl")
key = client.service.LogOnUser('myusername', 'mypassord')
print(key)
it is giving me an error as: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
While I am trying below command the URL works well and shows all the services it has
python -mzeep https://siteURL.asmx?wsdl
Please help to understand what is the reason above code is not working.
PS: I couldn't share site URL which I am trying to connect to.
Additional Info: The site/page is accessible only through intranet and I am testing locally from intranet itself.
Traceback error:
Exception Type: ConnectionError at /music/mypersonalapi/
Exception Value: HTTPSConnectionPool(host='URL I have hidden', port=81):
Max retries exceeded with url: /ABC/XYZ/Logon.asmx
(Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0546E770>:
Failed to establish a new connection:
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',))
Please note: I have removed URL and Host information from my traceback due to confidentiality
What this does:
python -mzeep https://site/url.asmx?wsdl
is:
c = Client("https://site/url.asmx?wsdl")
c.wsdl.dump()
both alternatives are using port 443 since that is the default https port.
From your traceback we see
Exception Value: HTTPSConnectionPool(host='URL I have hidden', port=81):
which would have been similar to
python -mzeep https://site:81/url.asmx?wsdl
I.e. the command line and your code are not connecting to the same address (also note that port values less than 1024 requires system level permissions to use -- in case you're writing/controlling the service too).
The last line does say "..failed because the connected party did not properly respond after a period of time..", but that is not the underlying reason. In line 3 you can read
Max retries exceeded with url: /ABC/XYZ/Logon.asmx
in other words, you've tried (and failed) to log on too many times and the server is probably doubling the time it uses to respond every time you try (a well known mitigation strategy for "things" that fail to login multiple times -- i.e. look like an attack). The extended delay is most likely causing the error message you see at the bottom.
You'll need to wait a while, or reset your account for the service, and if the service is yours then perhaps turn off this feature during development?
Maybe this can help. I had the same connexion problem (Max retries exceeded...). I solved it by increasing the transport timeout.
client = Client(wsdl=wsdl, transport=Transport(session=session, timeout=120))

Categories

Resources