Skip to content

Instantly share code, notes, and snippets.

@dojiong
Created January 10, 2014 14:02
Show Gist options
  • Save dojiong/8352613 to your computer and use it in GitHub Desktop.
Save dojiong/8352613 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from socket import *
def client():
sock = socket(AF_INET, SOCK_STREAM)
sock.connect(('127.0.0.1', 7777))
sock.close()
if __name__ == '__main__':
client()
#!/usr/bin/env python
from socket import *
import select
import os
def server():
sock = socket(AF_INET, SOCK_STREAM)
sock.setblocking(False)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind(('127.0.0.1', 7777))
sock.listen(64)
for i in range(3):
if not os.fork():
break
pid = os.getpid()
print(pid, 'start')
ep = select.epoll()
ep.register(sock, select.EPOLLIN)
while True:
try:
evts = ep.poll()
except KeyboardInterrupt:
break
for fd, event in evts:
if fd == sock.fileno():
try:
cli, cliaddr = sock.accept()
except Exception as e:
print(pid, e)
continue
print(pid, 'got', cliaddr)
cli.close()
else:
continue
break
sock.close()
print(pid, 'end')
if __name__ == '__main__':
server()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment