blocking - nonblocking

もとに戻し方をよく忘れる。

import fcntl
import os
import socket

if __name__ == '__main__':
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', 78787))
    sock.listen(5)
    while 1:
        conn, addr = sock.accept()
        print "Connected by", addr
        fcntl.fcntl(conn.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) # nonblocking
        fcntl.fcntl(conn.fileno(), fcntl.F_SETFL)                # blocking
        while 1:
            try:
                msg = conn.recv(8192)
            except socket.error, error:
                print error
                continue
            if not msg: break
            print "Recieve:", msg
            conn.send(msg)
        print "connection close.", addr
        conn.close()