surfly/gevent — pull request #295 by chrismeyersfsu (2013-08-06 12:37:09+00:00)
Title: fix local unix socket logging failure to get requestor hostname/ip

Receive error when logging a request. This is due to the backing IPC being a UNIX socket passed in via wsgi.WSGIServer(sock, wsgi_application, spawn=None).serve_forever()

The attached pull request substitutes the requestor ip address with UNIX_SOCKET when the above case is encountered. It might nicer to substitute the unix socket path, if possible.

To recreate:
* django (can modify the gevent python script to do without)
* nginx proxy_pass
```
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
#proxy_pass http://localhost:8000/;
proxy_pass http://unix:/tmp/server.sock;
}
```
* gevent
```
#!/usr/bin/python

import sys, os

from gevent import wsgi
from gevent import socket
from gevent import monkey
#from pong import application

sys.stdout = sys.stderr

# Just in case
monkey.patch_all()

import pwd

# Get this so we can chown/chgrp the socket and let nginx read it
pe = pwd.getpwnam('www-data')

SOCK = '/tmp/server.sock'

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
os.remove(SOCK)
except OSError:
pass
sock.bind(SOCK)
os.chown(SOCK, pe.pw_uid, pe.pw_gid)
os.chmod(SOCK,0770)
sock.listen(256)

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

# Set up for Django
sys.path.insert(0,'/var/www/my_django_project/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_django_project.settings'

wsgi.WSGIServer(sock, application, spawn=None).serve_forever()
```

```
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0dev-py2.7-linux-x86_64.egg/gevent/baseserver.py", line 159, in _do_read
self.do_handle(*args)
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0dev-py2.7-linux-x86_64.egg/gevent/baseserver.py", line 130, in do_handle
self._handle(*args)
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0dev-py2.7-linux-x86_64.egg/gevent/pywsgi.py", line 654, in handle
handler.handle()
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0dev-py2.7-linux-x86_64.egg/gevent/pywsgi.py", line 184, in handle
result = self.handle_one_request()
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0dev-py2.7-linux-x86_64.egg/gevent/pywsgi.py", line 316, in handle_one_request
self.handle_one_response()
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0dev-py2.7-linux-x86_64.egg/gevent/pywsgi.py", line 514, in handle_one_response
self.log_request()
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0dev-py2.7-linux-x86_64.egg/gevent/pywsgi.py", line 460, in log_request
log.write(self.format_request() + '\n')
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0dev-py2.7-linux-x86_64.egg/gevent/pywsgi.py", line 472, in format_request
self.client_address[0] if self.client_address is not None else "",
IndexError: string index out of range
(('',),
) failed with IndexError
```