如何在Django项目中获取客户端端口号? [英] How do I get the client port number in a Django project?

查看:897
本文介绍了如何在Django项目中获取客户端端口号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 django 构建我的Web服务器,其他人作为客户端连接到我.现在,我需要知道客户端的端口号以区分它们.如果他们的浏览器打开两个具有相同链接的制表符",即两个页面但具有相同的链接,则我也必须区分它们.

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.

尽管我知道我可以使用 request.META ['REMOTE_ADDR'] 在django视图功能中获取客户端的IP,但这对于我来说还远远不够.

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.

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

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?

其他信息:

  • 我正在使用python 2.6和django 1.4
  • 我知道将为浏览器的每个TAB分配一个随机的唯一端口,以访问我的django网页端口. -请参阅此链接'网络服务器打开端口80,但浏览器有一个不同的,随机分配的端口.'我确实需要区分它们.因此,我的直观想法是使用IP数据包中的端口号.如果您有其他建议,也欢迎您.
  • 我在此处找到了类似问题 ,但我现在不使用Apache.这对于我来说可能很难配置,因此可能引起其他更复杂的问题.这可能会使这个简单的问题变得复杂.
  • I'm using python 2.6 and 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.
  • 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.

推荐答案

是的,经过几天的苦苦挣扎,我回答了一个有效的但很丑陋的解决方案,该解决方案涉及如何获取客户端端口" Django".

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

    在您的python26/Lib/SocketServer.py中找到
  1. ,找到def process_request_thread,添加 global gClientPort; gClientPort = client_address

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

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

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

这篇关于如何在Django项目中获取客户端端口号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆