本文整理汇总了Python中socket.socket.recvfrom方法的典型用法代码示例。如果您正苦于以下问题:Python socket.recvfrom方法的具体用法?Python socket.recvfrom怎么用?Python socket.recvfrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket.socket
的用法示例。
在下文中一共展示了socket.recvfrom方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_client
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import recvfrom [as 别名]
def run_client(server_address, server_port):
# Ping a UDP pinger server running at the given address
try:
client_socket = Socket(socket.AF_INET, socket.SOCK_DGRAM)
# Time out after 1 second
client_socket.settimeout(1.0)
print("Pinging", str(server_address), "on port", server_port)
for i in range(10):
client_socket.sendto("".encode(), (server_address, server_port))
try:
time_start = time.time()
_, _ = client_socket.recvfrom(1024)
time_stop = time.time()
except socket.timeout:
print("Packet lost")
else:
print("RTT(Round trip time): {:.3f}ms".format((time_stop - time_start) * 1000))
finally:
client_socket.close()
return 0
示例2: get_rtt
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import recvfrom [as 别名]
def get_rtt(server_address, server_port):
"""Ping a UDP ping server running at the given address
and show the ping status
"""
server = (server_address, server_port)
message = bytes("", 'UTF-8')
client_socket = Socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.settimeout(1.0)
try:
initial_time = current_milli_time()
client_socket.sendto(message, server)
client_socket.recvfrom(1024)
final_time = current_milli_time()
rtt = final_time - initial_time
except:
rtt = -1
client_socket.close()
return rtt
示例3: run_client
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import recvfrom [as 别名]
def run_client(server_address, server_port):
"""Ping a UDP pinger server running at the given address
"""
client_socket = Socket(socket.AF_INET, socket.SOCK_DGRAM)
message = "Ping"
client_socket.settimeout(1)
for i in range(0, 10):
start_time = time.time()
client_socket.sendto(message, (server_address, server_port))
try:
data, server = client_socket.recvfrom(1024)
end_time = time.time()
elapsed = end_time - start_time
print "%s, time: %f." % (data, elapsed)
except:
print "TIMEOUT!"
time.sleep(1)
return 0
示例4: run_server
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import recvfrom [as 别名]
def run_server(server_port):
"""Run the UDP pinger server
"""
# Create the server socket (to handle UDP requests using ipv4), make sure
# it is always closed by using with statement.
server_socket = Socket(socket.AF_INET, socket.SOCK_DGRAM)
# The socket stays connected even after this script ends. So in order
# to allow the immediate reuse of the socket (so that we can kill and
# re-run the server while debugging) we set the following option. This
# is potentially dangerous in real code: in rare cases you may get junk
# data arriving at the socket.
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Set the server port
server_socket.bind(("", server_port))
# Start accepting ping requests
print ("Ping server ready on port", server_port)
while True:
# Receive message and send one back
_, client_address = server_socket.recvfrom(1024)
server_socket.sendto("Pong".encode(), client_address)
return 0
示例5: run_server
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import recvfrom [as 别名]
def run_server(server_port):
# Run UDP pinger server
try:
server_socket = Socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('', server_port))
print("Ping server ready on port", server_port)
while True:
_, client_address = server_socket.recvfrom(1024)
server_socket.sendto("".encode(), client_address)
finally:
server_socket.close()
return 0
示例6: on_receive
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import recvfrom [as 别名]
def on_receive(self, sock: socket.socket):
"""
Handle raw data and wrap it with DataContainer
"""
try:
data, addr = sock.recvfrom(2 ** 16)
except ConnectionResetError:
return
except OSError:
return
try:
data = data.decode()
except UnicodeDecodeError:
self.logger.warning('error in decoding received data')
return
action = data[:3]
data = data[3:]
self.logger.debug('action: {}; addr: {}; data: {}'
.format(action, addr, data))
dc = DataContainer(address=addr, action=action, data=data)
self.call_handler(dc)
示例7: udp_reader
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import recvfrom [as 别名]
def udp_reader(s: socket, iqueue: Queue, size: int) -> None:
"""Read one or more packets from an UDP socket."""
data, peer = s.recvfrom(size)
iqueue.put_nowait((peer, data))