0

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.

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.

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?

Additional info:

  • 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.
Community
  • 1
  • 1
Charles
  • 115
  • 1
  • 8
  • There is a similar question [here][1] [1]: http://stackoverflow.com/questions/20316078/how-to-retrieve-remote-port-in-django – Gocht May 15 '15 at 14:45
  • I have seen this topic but the solution maybe not the best. So I make this question more straight forward here. And in google groups I have seen ridiculous answers that the client is using the same port as server to access my website... – Charles May 15 '15 at 14:49
  • 1
    Did you try with [sessions](https://docs.djangoproject.com/en/1.8/topics/http/sessions/) ? – Sylvain Biehler May 15 '15 at 14:51
  • Not familiar with it. Could you shed more light? – Charles May 15 '15 at 14:56
  • 1
    I'm a bit confused with your statement 'I know every TAB of a browser will open a unique port to access my django web page'. Your Django application listens to only one port. Doesn't sessions mechanism fit your needs in distinguishing users? – Ihor Pomaranskyy May 15 '15 at 15:08
  • What server do you use? Maybe it's better to configure the server to pass the port as header (like in apache+mod_wsgi) solution. Also there is similar [question](http://stackoverflow.com/questions/368653/how-to-differ-sessions-in-browser-tabs) (but with Java server). They suggest to use `window.name` or `window.sessionStorage` in JS. – Igor May 15 '15 at 15:11
  • @IgorPomaranskiy see this link, '[The web server opens port 80, but the browser has a different, randomly-assigned port. ](http://stackoverflow.com/questions/2957757/how-can-an-application-use-port-80-http-without-conflicting-with-browsers)' – Charles May 15 '15 at 15:15
  • I'm using django python server. I just enter the command 'python manage.py runserver 0.0.0.0:80'. Then other people can access my 'server'. – Charles May 15 '15 at 15:18
  • 1
    In HTTP, connections are NOT persistent. So, these ports are different on every connection, so you can't rely on them as identifiers of _users_. – Ihor Pomaranskyy May 15 '15 at 15:22
  • @Bilou06 I tried session. But with session, all tabs in one browser have the same session_key. How should I do to get different session keys for each tab? – Charles May 18 '15 at 08:07

3 Answers3

3

while I debug the django , I find this

request.environ["wsgi.input"].raw._sock.getpeername()

maybe it can work

yahui liu
  • 41
  • 5
  • This may vary between Django(/python?) versions: for me `request.environ["wsgi.input"].stream.raw._sock.getpeername()` worked – andrewdotn Jan 13 '21 at 20:49
1

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

  1. in your python26/Lib/SocketServer.py, find def process_request_thread,add global 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.

Charles
  • 115
  • 1
  • 8
0
  1. Your assumption about 'every user connection opens connection at unique port' is wrong. All users are using the same port to connect.
  2. 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.

Here is documentation on sessions:

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

Ihor Pomaranskyy
  • 5,437
  • 34
  • 37
  • Yes you are right. They access my server via port 80 which I opened with python & django. But their browser has a different, randomly assigned port to access my port 80. Which I can detect from their IP packets sent here to me, it's in the IP header I received. – Charles May 15 '15 at 15:21
  • 3
    The OP is correct. Every connection originates from a different port. It __leads__ to the same IP & port on the server - but that's not what was asked. Try netstat on your machine with a few browser tabs open. You are right though that using this as session-like key is not a good idea. – deets May 15 '15 at 15:22
  • 2
    You're confusing the server port with the port the browser is listening on. See the question OP linked. That said, you're not wrong about using sessions here. – Two-Bit Alchemist May 15 '15 at 15:22
  • I got it. Every connection is two-way, but the ports, opened on client-side anyway can't be used to distinguish users, since they are can be (and usually are) different for every page user opens. – Ihor Pomaranskyy May 15 '15 at 15:25
  • Yes yes! I need to distinguish **every page** user opens. – Charles May 15 '15 at 15:27
  • 1
    Can you please add to the topic some more _global_ description of what you are going to implement? Maybe I'm wrong, but for now it looks like you are trying to use wrong instruments to solve the issue which is already solved. – Ihor Pomaranskyy May 15 '15 at 15:29
  • 1
    To be more specific, I want you to answer the question: what for do you need the client-side port numbers? – Ihor Pomaranskyy May 15 '15 at 15:31
  • @IgorPomaranskiy The reason I need the client port number is: If their browser opens two 'Tabs' of the same link, i.e. two pages but the same link, I also have to distinguish them. So I can use their IP+port number. The reason behind this reason is I'm displaying per-page user defined data. The different page will send different data then I display it. – Charles May 15 '15 at 15:37
  • May I ask, what for do you need to distinguish connections from different tabs? – Ihor Pomaranskyy May 15 '15 at 15:41
  • @IgorPomaranskiy I think I have written all the thing I could think to explain.. Maybe I didn't understand the whole part of your question.. sorry..I think the reason is I'm displaying per-page user defined data. The different page will send different data then I display it. Not sure if this clear enough.. – Charles May 15 '15 at 15:44
  • 1
    Look, there _are_ some ways to distinguish different 'threads' of user workflow. For example, you can add some unique GET-parameter to all links on your page, so when user clicks the link — you can distinguish clicks from different tabs. And this solution will be 100% in all cases. But such solution is ugly, and I doubt if you'll find something better. Because in web-applications we usually don't distinguish connections from differen tabs. – Ihor Pomaranskyy May 15 '15 at 15:52
  • 1
    FYI, session IDs are never passed as a GET parameter in Django. This is an intentional design decision to make Django applications more secure: https://docs.djangoproject.com/en/1.8/topics/http/sessions/#session-ids-in-urls If you update your answer, I'll upvote it for sure. – Bobort Oct 19 '17 at 15:33
  • 1
    @Bobort, yes, you are right. It was a memory from my previous life (PHP-related). Thank you for correction. The answer is fixed. No upvoting required, really. – Ihor Pomaranskyy Oct 20 '17 at 19:53