本文整理汇总了Python中socket.makefile函数的典型用法代码示例。如果您正苦于以下问题:Python makefile函数的具体用法?Python makefile怎么用?Python makefile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了makefile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: client_protocol
def client_protocol(socket):
connect_write_to_server = socket.makefile('w')
protocol = 'I32CFP_HELLO\n'
connect_write_to_server.write(protocol)
connect_write_to_server.flush()
receive_ready = socket.makefile('r')
protocol = receive_ready.readline()
if protocol == 'READY\n':
print('The server is ready to start the game')
pass
else:
socket.close()
print('server not speaking the same protocols,close connection.\n')
return
开发者ID:409230250,项目名称:Programming-with-Software-Libraries-on-Python,代码行数:14,代码来源:Lab2_Console+Only.py
示例2: handle_request
def handle_request(socket, client_address, server):
s = ServerInfo(software=server_version, multiconnection=True, multithread=False, multiprocess=False)
c = ConnectionInfo(server=s)
c.server.addr = LazyAddress(socket.family, lambda: socket.getsockname())
c.remote.addr = Address(socket.family, client_address)
c.socket = socket
with socket.makefile('r') as input:
with socket.makefile('w') as output:
app(input, output, sys.stderr, c)
示例3: receive_protocols
def receive_protocols(socket):
file_in = socket.makefile('r')
protocols = file_in.readline()
file_in.close()
if protocols == 'I32CFP_HELLO\n':
file_out = socket.makefile('w')
file_out.write('READY\n')
file_out.flush()
file_out.close()
pass
else:
socket.close()
print('Client is not speaking the same protocols.Closing connection.\n')
return
开发者ID:409230250,项目名称:Programming-with-Software-Libraries-on-Python,代码行数:14,代码来源:Lab2_Console+Only.py
示例4: handle
def handle(self, socket, address):
"""
If not a leader, a node will simply return a single item list pointing
to the leader. Otherwise, it will add the host of the connected client
to the cluster roster, broadcast to all nodes the new roster, and wait
for keepalives. If no keepalive within timeout or the client drops, it
drops it from the roster and broadcasts to all remaining nodes.
"""
self.logger.debug('New connection from %s:%s' % address)
if not self.manager.is_leader:
socket.send(json.dumps({'leader': self.manager.client.leader_address[0],
'port': self.manager.client.leader_address[1]}))
socket.close()
self.logger.debug("Redirected to %s:%s" % self.manager.client.leader_address)
else:
socket.send(self._cluster_message())
sockfile = socket.makefile()
name = sockfile.readline()
if not name:
return
if name == '\n':
name = address[0]
else:
name = name.strip()
self._update(add={'host': name, 'socket': socket})
# TODO: Use TCP keepalives
timeout = self._client_timeout(socket)
for line in util.line_protocol(sockfile, strip=False):
timeout.kill()
timeout = self._client_timeout(socket)
socket.send('\n')
self.logger.debug("Keepalive from %s:%s" % address)
self.logger.debug("Client disconnected from %s:%s" % address)
self._update(remove=name)
示例5: wait_server_move
def wait_server_move(mode,column,socket):
output = socket.makefile('w')
output.write(mode+ ' ' + str(column)+'\n')
output.flush()
output.close()
print('Waiting for server to move or update.')
pass
示例6: __init__
def __init__(self, server, socket):
self.server = server
self.socket = socket
self.socket_file = socket.makefile('rb', 0)
self.compression_enabled = False
self.user_uuid = None
self.user_name = None
示例7: listen_on_socket
def listen_on_socket(socket, *args):
if socket is None:
return
command = ""
file = socket.makefile('rwb')
while True:
try:
data = file.read(1)
if data == b'':
sleep(0.001)
continue
else:
data = data.decode('ascii')
if data == AtHomeProtocol['end_of_communication']:
print('Communication ended', file=sys.stderr)
return
if data != AtHomeProtocol['end_of_command']:
command += data
else:
command = ""
continue
if command.endswith(AtHomeProtocol['end_of_line']):
command = command[0:-2]
if command in AtHomeCommands:
AtHomeCommands[command](file)
else:
command = ""
raise NameError('[Unknown Command] %s' % command)
command = ""
except EOFError:
file.close()
socket.close()
return
except Exception as e:
print('[Exception] %s' % e, file=sys.stderr)
示例8: __init__
def __init__(self, socket):
"""Keeps track of the socket and the file posing as the socket"""
self.socket = socket
self.stream = socket.makefile()
self.active = True
self.editor = 'Emacs'
self.name = 'Richard Stallman'
示例9: mangodb
def mangodb(socket, address):
socket.sendall('HELLO\r\n')
client = socket.makefile()
output = open(os.devnull, 'w')
lock = threading.Lock()
wait = threading.Condition(lock)
while 1:
line = client.readline()
if not line:
break
cmd_bits = line.split(' ', 1)
cmd = cmd_bits[0]
if cmd == 'BYE':
break
if cmd == 'WAIT':
wait.wait()
continue
if len(cmd_bits) > 1:
lock.acquire(True)
output.write(cmd_bits[1])
if MANGODB_DURABLE:
output.flush()
os.fsync(output.fileno())
data = '42' if MANGODB_EVENTUAL else \
os.urandom(1024).encode('string-escape')
lock.release()
client.write('OK' + data + '\r\n')
client.flush()
示例10: WrapAndSendPayload
def WrapAndSendPayload(cls, socket, payload):
'''
Send payload, true if success, false if failed
'''
try:
# From SocketServer.py
# wbufsize = 0, flush immediately
wbufsize = -1
# Convert
socket_message = SocketMessage(payload)
wfile = socket.makefile('wb', wbufsize)
# Write the message
wfile.write(struct.pack(fmt, socket_message.magic))
# Need to send the packed version
# print 'Sent ', socket_message.magic
wfile.write(struct.pack(fmt, socket_message.payload_size))
# print 'Sent ', socket_message.payload_size
wfile.write(payload)
# print 'Sent ', payload
wfile.flush()
wfile.close() # Close file object, not close the socket
return True
except Exception as e:
_L.error('Fail to send message %s', e)
return False
示例11: run
def run(self):
"""Starts listening and accepting connection requests.
This method is called when invoking `CallbackServer.start()`. A
CallbackServer instance is created and started automatically when
a :class:`JavaGateway <py4j.java_gateway.JavaGateway>` instance is
created.
"""
try:
with self.lock:
self.is_shutdown = False
logger.info('Callback Server Starting')
self.server_socket.listen(5)
logger.info('Socket listening on {0}'.
format(smart_decode(self.server_socket.getsockname())))
while not self.is_shutdown:
socket, _ = self.server_socket.accept()
input = socket.makefile('rb', 0)
connection = CallbackConnection(self.pool, input, socket,
self.gateway_client)
with self.lock:
if not self.is_shutdown:
self.connections.append(connection)
connection.start()
else:
connection.socket.shutdown(socket.SHUT_RDWR)
connection.socket.close()
except Exception:
if self.is_shutdown:
logger.info('Error while waiting for a connection.')
else:
logger.exception('Error while waiting for a connection.')
示例12: wait_player_move
def wait_player_move(mode, col,socket):
output = socket.makefile('w')
output.write(mode+ ' ' + str(col)+'\n')
output.flush()
output.close()
print('waiting for player to move')
pass
示例13: __init__
def __init__(self, server, socket):
"""
Override BaseHTTPRequestHandler.__init__(): we need to be able
to have (potentially) multiple handler objects at a given time.
Inputs:
server -- server object to handle requests for
socket -- socket connection
"""
## Settings required by BaseHTTPRequestHandler
self.rfile = socket.makefile("rb", -1)
self.wfile = socket.makefile("wb", 0)
self.connection = socket
self.client_address = (server.host, server.port)
self._server = server
示例14: wait_server_move
def wait_server_move(mode,drop,socket):
print('runing wait server move')
output = socket.makefile('w')
output.write(mode+ ' ' + str(drop)+'\n')
output.flush()
output.close()
print('waiting for server to move')
pass
示例15: run
def run(self, room, program, password):
socket = self.connect_to_service()
socket_fd = socket.makefile()
skip_hello(socket_fd)
send("run", socket)
send(password, socket)
send(room, socket)
send(program, socket)
return readline(socket_fd)