How to use Flask get client's port?

前端 未结 2 1867
猫巷女王i
猫巷女王i 2021-01-14 15:51

I\'m trying to build a simple torrent-tracker with Flask, but I meet a problem.

If client in NAPT network, the port, which included in request is in

相关标签:
2条回答
  • 2021-01-14 16:12

    If Flask is behind a reverse proxy,

    request.environ.get('REMOTE_PORT')
    

    will not give you what you want, you are going to get the port used by the reverse proxy.

    If using Nginx, add this bold line to your config file:

    server {
        listen 80;
        server_name _;
        location / {
            proxy_pass ...;
            proxy_set_header WHATEVER $remote_port;
        } 
    } 

    Then you can get the client port with :

    request.headers.get('WHATEVER')
    
    0 讨论(0)
  • 2021-01-14 16:34

    You can get it from request.environ

    request.environ.get('REMOTE_PORT')
    
    0 讨论(0)
提交回复
热议问题