Is there any way to find out what port a Django instance is listening on from within the code?
有没有办法找出Django实例在代码中侦听的端口?
4 个解决方案
#1
9
You can get the info through the HttpRequest. Checkout the Django Docs here.
您可以通过HttpRequest获取信息。在这里查看Django文档。
This can be accessed through the META attribute which is a dictionary containing the HTTP header info.
这可以通过META属性访问,该属性是包含HTTP头信息的字典。
Example:
def someView(request):
#Try printing to screen
print request.META['SERVER_PORT']
...
return(response)
#2
3
maybe request.META['SERVER_PORT']
or are you not in a view?
或者你不在视野中?
#3
1
Request's have a method build in specifically for this.
请求有一个专门为此建立的方法。
def someView(request):
print(request.get_port())
#4
0
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.)
我发现如果您需要知道视图外的端口号或IP地址(例如,在models.py中),这可能会有所帮助。
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
#1
9
You can get the info through the HttpRequest. Checkout the Django Docs here.
您可以通过HttpRequest获取信息。在这里查看Django文档。
This can be accessed through the META attribute which is a dictionary containing the HTTP header info.
这可以通过META属性访问,该属性是包含HTTP头信息的字典。
Example:
def someView(request):
#Try printing to screen
print request.META['SERVER_PORT']
...
return(response)
#2
3
maybe request.META['SERVER_PORT']
or are you not in a view?
或者你不在视野中?
#3
1
Request's have a method build in specifically for this.
请求有一个专门为此建立的方法。
def someView(request):
print(request.get_port())
#4
0
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.)
我发现如果您需要知道视图外的端口号或IP地址(例如,在models.py中),这可能会有所帮助。
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