Gabriel
Gabriel

Reputation: 51

REMOTE_PORT through WSGI

I'm currently trying to get information from clients in a web application written with bottle.py. Bottle.py let us access the CGI variables, as defined in WSGI specs through the environ variable. But these specs does not list remote_port as a mandatory nor 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 the remote_port information in any other mean: accessing the raw socket directly to retrieve the client_address, through bottle.py, or by any other mean

Thanks, Gabriel

Upvotes: 4

Views: 1767

Answers (2)

Anto
Anto

Reputation: 7222

It seems to depend on how you serve your application.

For instance, the Django developpement server (runserver) won't provide you with the remote port. Meanwhile, I was able to retrieve it with an app served on Apache and mod_wsgi and, apparently, the CherryPy server also provides it.

The weird thing is that, just like you said, it's not part of the WSGI specification (PEP 333).

Upvotes: 0

PoL
PoL

Reputation: 11

I just made 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 get the port in environ['REMOTE_PORT'].

Here the full content 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'
}

Upvotes: 1

Related Questions