Package: proxy-protocol
proxy-protocolPROXY protocol library withasyncioserver implementation.SpecificationAPI ReferenceDocker ImageTable of ContentsInstall and UsageServer UsageEcho ServerProxy ServerDevelopment and TestingType HintingInstall and Usage$pipinstallproxy-protocolIntegration with anasyncio.start_serverbased server is intended to be
extremely simple. Here is an example, which will detect PROXY protocol v1 or
v2.fromproxyprotocolimportProxyProtocolfromproxyprotocol.detectimportProxyProtocolDetectfromproxyprotocol.readerimportProxyProtocolReaderfromproxyprotocol.sockimportSocketInfoasyncdefrun(host:str,port:int)->None:pp_detect=ProxyProtocolDetect()callback=ProxyProtocolReader(pp_detect).get_callback(on_connection)server=awaitasyncio.start_server(callback,host,port)asyncwithserver:awaitserver.serve_forever()asyncdefon_connection(reader:StreamReader,writer:StreamWriter,info:SocketInfo)->None:print(info.family,info.peername)# ... continue using connectionTo simplify PROXY protocol use based on configuration, the version can also be
read from a string.fromproxyprotocol.versionimportProxyProtocolVersionpp_noop=ProxyProtocolVersion.get(None)pp_detect=ProxyProtocolVersion.get('detect')pp_v1=ProxyProtocolVersion.get('v1')pp_v2=ProxyProtocolVersion.get('v2')Thepp_noopobject in this example is a special case implementation that does
not read a PROXY protocol header from the stream at all. It may be used to
disable PROXY protocol use without complicating your server code.You can also check out theproxyprotocol-echoreference implementation.
If you configure your proxy to send PROXY protocol tolocalhost:10007, you
can see it in action:$proxyprotocol-echo--help
$proxyprotocol-echodetect
$proxyprotocol-echonoopServer UsageTwo basic server implementations are included for reference. Using the two
together can demonstrate the process end-to-end: useproxyprotocol-serverto proxy connections with a PROXY protocol header toproxyprotocol-echo,
which then displays the original connection information.Thehostname:portarguments used by both types of servers are parsed by theAddressclass, which allows for customization of SSL/TLS and PROXY
protocol versions.Echo ServerTheproxyprotocol-echoserver expects inbound connections to provide a PROXY
protocol header indicating the original source of the connection. After the
header, all received data will be echoed back to the client.proxyprotocol-echo--help
proxyprotocol-echo# run the serverProxy ServerTheproxyprotocol-serverserver proxies inbound connections to another
host/port endoint, prefixing the outbound connection with a PROXY protocol
header to indicate the original connection information.proxyprotocol-server--help
proxyprotocol-server--servicelocalhost:10000localhost:10007Development and TestingYou will need to do some additional setup to develop and test plugins. InstallHatchto use the CLI examples below.Run all tests and linters:$hatchruncheckBecause this project supports several versions of Python, you can use the
following to run the checks on all versions:$hatchrunall:checkType HintingThis project makes heavy use of Python'stype hintingsystem, with the
intention of a clean run ofmypyin strict mode:mypy proxyprotocol testNo code contribution will be accepted unless it makes every effort to use type
hinting to the extent possible and common in the rest of the codebase.