avoid about-to-be-deprecated getClientIP if we can

Use the replacement, getClientAddress.  But have a fallback to
getClientIP to keep supporting older versions of Twisted.
This commit is contained in:
Jean-Paul Calderone 2018-04-20 16:03:19 -04:00
parent edf3c7aac7
commit 8eb83bbfb9

View File

@ -2,6 +2,10 @@ import re, time
from twisted.application import service, strports, internet from twisted.application import service, strports, internet
from twisted.web import http, static from twisted.web import http, static
from twisted.internet import defer from twisted.internet import defer
from twisted.internet.address import (
IPv4Address,
IPv6Address,
)
from nevow import appserver, inevow from nevow import appserver, inevow
from allmydata.util import log, fileutil from allmydata.util import log, fileutil
@ -119,7 +123,7 @@ class MyRequest(appserver.NevowRequest):
"web: %(clientip)s %(method)s %(uri)s %(code)s " "web: %(clientip)s %(method)s %(uri)s %(code)s "
"%(length)s%(error)s" "%(length)s%(error)s"
), ),
clientip=self.getClientIP(), clientip=_get_client_ip(self),
method=self.method, method=self.method,
uri=uri, uri=uri,
code=self.code, code=self.code,
@ -130,6 +134,18 @@ class MyRequest(appserver.NevowRequest):
) )
def _get_client_ip(request):
try:
get = request.getClientAddress
except AttributeError:
return request.getClientIP()
else:
client_addr = get()
if isinstance(client_addr, (IPv4Address, IPv6Address)):
return client_addr.host
return None
class WebishServer(service.MultiService): class WebishServer(service.MultiService):
name = "webish" name = "webish"