如何在Django项目中获取客户端端口号?

时间:2021-04-02 18:12:00

I am using django to build my web server, other people connect to me as clients. Now I need to know the clients' port number to distinguish them. If their browser opens two 'Tabs' of the same link, i.e. two pages but the same link, I also have to distinguish them.

我正在使用django来构建我的Web服务器,其他人作为客户端连接到我。现在我需要知道客户端的端口号以区分它们。如果他们的浏览器打开两个相同链接的“标签”,即两个页面但相同的链接,我也必须区分它们。

Although I know I can use request.META['REMOTE_ADDR'] to get the client's IP in my django view function, but this realy is not enough for me.

虽然我知道我可以使用request.META ['REMOTE_ADDR']在我的django视图函数中获取客户端的IP,但这对我来说还不够。

Then I studied some TCP/IP basics and then I know that in TCP/IP layer, every IP packet has an IP header which contains the client's port number. But how can I access it in django?

然后我研究了一些TCP / IP基础知识,然后我知道在TCP / IP层,每个IP数据包都有一个IP头,其中包含客户端的端口号。但是如何在django中访问它?

Additional info:

附加信息:

  • I'm using python 2.6 and django 1.4
  • 我正在使用python 2.6和django 1.4
  • I know every TAB of a browser will be allocated a random unique port to access my django web page port. -- see this link 'The web server opens port 80, but the browser has a different, randomly-assigned port.' I really need to distinguish them. So my intuitive thoughts is to use the port number in the IP packet. If you have any other suggestion, also welcome.
  • 我知道浏览器的每个TAB都会被分配一个随机的唯一端口来访问我的django网页端口。 - 请参阅此链接'Web服务器打开端口80,但浏览器有一个不同的,随机分配的端口。'我真的需要区分它们。所以我直观的想法是使用IP数据包中的端口号。如果您有任何其他建议,也欢迎。
  • I have found the similar question here, but I am not using Apache now. And this may be hard for me to config so maybe causing other more complex questions. This might make this simple question complex.
  • 我在这里找到了类似的问题,但我现在没有使用Apache。这对我来说可能很难配置,所以可能导致其他更复杂的问题。这可能使这个简单的问题变得复杂。

2 个解决方案

#1


1  

  1. Your assumption about 'every user connection opens connection at unique port' is wrong. All users are using the same port to connect.
  2. 您对“每个用户连接在唯一端口打开连接”的假设是错误的。所有用户都使用相同的端口进行连接。
  3. To distinguish users Django (and almost every other frameworks) is using sessions. Every user gets a cookie with his unique session ID, and this cookie is passed to a server on every connection, so the application can distinguish users.
  4. 为了区分用户Django(以及几乎所有其他框架)正在使用会话。每个用户都使用其唯一的会话ID获取cookie,并且此cookie在每个连接上传递给服务器,因此应用程序可以区分用户。

Here is documentation on sessions:

以下是会话文档:

https://docs.djangoproject.com/en/1.8/topics/http/sessions/

https://docs.djangoproject.com/en/1.8/topics/http/sessions/

#2


0  

Yes, after days of struggling, I answer it, with a working, but ugly solution on 'how to get client port in Django'.

是的,经过几天的挣扎之后,我回答一下,“如何在Django中获得客户端端口”的解决方案是一个有效且又丑陋的解决方案。

  1. in your python26/Lib/SocketServer.py, find def process_request_thread,add global gClientPort; gClientPort = client_address

    在你的python26 / Lib / SocketServer.py中,找到def process_request_thread,添加全局gClientPort; gClientPort = client_address

  2. use this global value in yout project. Its format is ('12.34.56.78',55437) for example. 55437 is the port number.

    在yout项目中使用此全局值。例如,它的格式是('12 .34.56.78',55437)。 55437是端口号。

#1


1  

  1. Your assumption about 'every user connection opens connection at unique port' is wrong. All users are using the same port to connect.
  2. 您对“每个用户连接在唯一端口打开连接”的假设是错误的。所有用户都使用相同的端口进行连接。
  3. To distinguish users Django (and almost every other frameworks) is using sessions. Every user gets a cookie with his unique session ID, and this cookie is passed to a server on every connection, so the application can distinguish users.
  4. 为了区分用户Django(以及几乎所有其他框架)正在使用会话。每个用户都使用其唯一的会话ID获取cookie,并且此cookie在每个连接上传递给服务器,因此应用程序可以区分用户。

Here is documentation on sessions:

以下是会话文档:

https://docs.djangoproject.com/en/1.8/topics/http/sessions/

https://docs.djangoproject.com/en/1.8/topics/http/sessions/

#2


0  

Yes, after days of struggling, I answer it, with a working, but ugly solution on 'how to get client port in Django'.

是的,经过几天的挣扎之后,我回答一下,“如何在Django中获得客户端端口”的解决方案是一个有效且又丑陋的解决方案。

  1. in your python26/Lib/SocketServer.py, find def process_request_thread,add global gClientPort; gClientPort = client_address

    在你的python26 / Lib / SocketServer.py中,找到def process_request_thread,添加全局gClientPort; gClientPort = client_address

  2. use this global value in yout project. Its format is ('12.34.56.78',55437) for example. 55437 is the port number.

    在yout项目中使用此全局值。例如,它的格式是('12 .34.56.78',55437)。 55437是端口号。