Find out which instance of the Django port is working?

Is there any way to find out which port Django is listening from code?

+6
source share
3 answers

You can get information through HttpRequest. Checkout Django Docs here .

This can be accessed through the META attribute, which is a dictionary containing HTTP header information.

Example:

def someView(request): #Try printing to screen print request.META['SERVER_PORT'] ... return(response) 
+9
source

maybe request.META['SERVER_PORT']

or don't you see?

+3
source

I found this to be useful if you need to know the port number or IP address from the view (e.g. in models.py).

 import sys import socket logger.error(socket.gethostbyname(socket.gethostname())+"----"+sys.argv[-1]) 

This will give you the output as shown below:

 192.168.1.222----0.0.0.0:8000 
0
source

Source: https://habr.com/ru/post/903707/


All Articles