'write close data to the server.
reason: the reason to close. This must be string.'

def write_close(self, code=None, reason=None):

    if ((code is None) and (reason is not None)):
        code = 1000
    if (code is None):
        close_data = ''
    else:
        close_data = struct.pack('>H', code)
    if (reason is not None):
        close_data += reason
    self._write_frame(True, self.OPCODE_CLOSE, close_data)
