本文整理汇总了Python中socket.socket.sendto方法的典型用法代码示例。如果您正苦于以下问题:Python socket.sendto方法的具体用法?Python socket.sendto怎么用?Python socket.sendto使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket.socket
的用法示例。
在下文中一共展示了socket.sendto方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_client
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import sendto [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: run_client
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import sendto [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
示例3: get_rtt
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import sendto [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
示例4: run_server
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import sendto [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: udp_writer
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import sendto [as 别名]
async def udp_writer(s: socket, oqueue: Queue) -> None:
"""Forward packets to the UDP socket."""
while True:
peer, data = await oqueue.get()
try:
s.sendto(data, peer)
finally:
oqueue.task_done()
示例6: run_server
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import sendto [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
示例7: send_data
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import sendto [as 别名]
def send_data (sock : socket.socket, addr, data : str):
ints = [int(data[i : i + 2], 16) for i in range(0, len(data), 2)] # add endian conversion if necessary
sock.sendto(bytes(ints), addr)