Connection refused Django requests-module - python

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

Related

How to make multiple requests on flask server in 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.

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.

New Connection Error when trying to use requests.get at python 3.7

well i've been trying to download a File directly from a URL, the thing is, when I do it at home it works perfectly fine, but when I try to run it at my company's server it doesn't work.
My code is:
import requests
url = "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php"
response = requests.get(url, verify=False)
url = open("teste.zip", "w")
url.write(response.content)
url.close()
and the error message that i get is:
HTTPSConnectionPool(host='servicos.ibama.gov.br', port=443): Max retries exceeded with url: /ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000001F2F792C0F0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
I konw that we use proxy here, i've been looking for solutions for this problem and I could see that this is relevant, but I couldn't find something to do with this information

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 Requests Deployment

In on of my Django view´s I am making a request to another webserver via requests, this code works perfect when I run the code with the development server. It looks like that:
import requests
r = requests.post('http://www.example.org')
return r.text
But when I Deploy this an apaches www-data user is running it it throws this error:
ConnectionError
HTTPConnectionPool(host='www.example.com', port=80): Max retries exceeded
with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection
object at 0x7f23e917ba58>:Failed to establish a new connection:[Errno -2]
Name or Service not known'))
But I can run the code as root user from a seperate script on the Deployment server without a problem this is why i think it is a permission problem. But i have no Idea how to solve this.
Thanks in advance!

Categories

Resources