REMOTE_PORT via WSGI

I am currently trying to get information from clients in a web application written by bottle.py . Bottle.py allows us to access CGI variables, as defined in WSGI , through an environment variable. But these specifications do not specify remote_port as a required or optional variable.

@get('/echo/) def echo(): values = {} for i in request.environ: values[i] = str(request.environ[i] return values 

So I'm wondering if there is a way to access remote_port information in any other way: to access raw sockets directly to get client_address, via bottle.py or any other average value

Thanks Gabriel

+4
source share
2 answers

I just ran a test with apache-2.4 mod_wsgi and this simple wsgi script:

 def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) yield str(environ) 

And at least with this mod_wsgi, you will get the port in environ['REMOTE_PORT'] .

Here is the full text of environ :

 { 'mod_wsgi.listener_port':'80', 'mod_wsgi.listener_host':'', 'CONTEXT_DOCUMENT_ROOT':'/usr/htdocs', 'SERVER_SOFTWARE':'Apache', 'SCRIPT_NAME':'', 'mod_wsgi.enable_sendfile':'0', 'mod_wsgi.handler_script':'', 'SERVER_SIGNATURE':'<address>Apache Server at 127.0.0.1 Port 80</address>\n', 'REQUEST_METHOD':'GET', 'PATH_INFO':'/', 'SERVER_PROTOCOL':'HTTP/1.1', 'QUERY_STRING':'', 'HTTP_USER_AGENT':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.58 Safari/537.36', 'HTTP_CONNECTION':'keep-alive', 'SERVER_NAME':'127.0.0.1', 'REMOTE_ADDR':'127.0.0.1', 'mod_wsgi.queue_start':'1409123400512619', 'mod_wsgi.request_handler':'wsgi-script', 'apache.version':(2, 4, 10), 'wsgi.url_scheme':'http', 'PATH_TRANSLATED':'/opt/wsgi/wsgi.py/', 'SERVER_PORT':'80', 'wsgi.multiprocess':True, 'mod_wsgi.input_chunked':'0', 'SERVER_ADDR':'127.0.0.1', 'DOCUMENT_ROOT':'/usr/htdocs', 'mod_wsgi.process_group':'wsgi_local', 'mod_wsgi.daemon_connects':'1', 'SCRIPT_FILENAME':'/opt/wsgi/wsgi.py', 'SERVER_ADMIN':'[no address given]', 'wsgi.input':<mod_wsgi.Input object at 0x7fd6fc5d24b0>, 'HTTP_HOST':'127.0.0.1', 'CONTEXT_PREFIX':'', 'wsgi.multithread':True, 'mod_wsgi.callable_object':'application', 'HTTP_CACHE_CONTROL':'max-age=0', 'mod_wsgi.daemon_restarts':'0', 'REQUEST_URI':'/', 'HTTP_ACCEPT':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'wsgi.file_wrapper':<type 'mod_wsgi.FileWrapper'>, 'wsgi.version':(1, 0), 'GATEWAY_INTERFACE':'CGI/1.1', 'wsgi.run_once':False, 'wsgi.errors':<mod_wsgi.Log object at 0x7fd6fc5e8930>, 'REMOTE_PORT':'32915', 'HTTP_ACCEPT_LANGUAGE':'fr,en-US;q=0.8,en;q=0.6', 'REQUEST_SCHEME':'http', 'mod_wsgi.version':(4,2,7), 'mod_wsgi.script_start':'1409123400512718', 'mod_wsgi.application_group':'127.0.0.1|', 'mod_wsgi.script_reloading':'1', 'mod_wsgi.request_start':'1409123400512528', 'HTTP_ACCEPT_ENCODING':'gzip,deflate,sdch', 'UNIQUE_ID':' U-2ESH8AAAEAACYQU@MAAABE ', 'mod_wsgi.daemon_start':'1409123400512693' } 
+1
source

It seems that it depends on how you maintain your application.

For example, the Django development server ( runserver ) will not provide you with a remote port. In the meantime, I was able to get it using an application that was running on Apache and mod_wsgi and apparently CherryPy server also provides it.

The strange thing is that, as you said, this is not part of the WSGI specification (PEP 333) .

0
source

All Articles