Skip to content

Instantly share code, notes, and snippets.

@mattfox
Created June 29, 2016 03:40
Show Gist options
  • Save mattfox/3ea31317a050b163a90b36d88c3e252b to your computer and use it in GitHub Desktop.
Save mattfox/3ea31317a050b163a90b36d88c3e252b to your computer and use it in GitHub Desktop.
python-netfilterqueue/gevent proof of concept
#!/usr/bin/env python
from gevent.server import StreamServer
from netfilterqueue import NetfilterQueue
from gevent import monkey; monkey.patch_socket()
def print_and_accept(pkt):
print(pkt)
pkt.accept()
def echo(socket, address):
print('New connection from %s:%s' % address)
socket.sendall(b'Welcome to the echo server! Type quit to exit.\r\n')
# using a makefile because we want to use readline()
rfileobj = socket.makefile(mode='rb')
while True:
line = rfileobj.readline()
if not line:
print("client disconnected")
break
if line.strip().lower() == b'quit':
print("client quit")
break
socket.sendall(line)
print("echoed %r" % line)
rfileobj.close()
if __name__ == '__main__':
server = StreamServer(('127.0.0.1', 16000), echo)
print('Starting echo server on port 16000')
server.start()
nfqueue = NetfilterQueue()
nfqueue.bind(1, print_and_accept)
try:
nfqueue.run2()
except KeyboardInterrupt:
print('')
nfqueue.unbind()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment