[Solved]-Find out what port Django instance is running on?

13👍

You can get the info through the HttpRequest. Checkout the Django Docs here.

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

Example:

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

4👍

maybe request.META['SERVER_PORT']

or are you not in a view?

👤second

3👍

I found this might be helpful if you need to know the port number or IP address out of the view(in models.py for example.)

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

This will give you an output like below:

192.168.1.222----0.0.0.0:8000
👤HiRob

2👍

Request’s have a method build in specifically for this.

def someView(request):
    print(request.get_port())
👤Cody

Leave a comment