当前位置: 首页 > 图文教程 > 脚本技术 > Python > Python 网络编程起步(Socket发送消息)
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", 8081))
while True:
# Receive up to 1,024 bytes in a datagram
data, addr = s.recvfrom(1024)
print "Received:", data, "from", addr
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 8081
host = "localhost"
while True:
msg = raw_input()
s.sendto(msg, (host, port))
评论 (0) All