您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在这个实战案例中,我们将使用Python进行Linux网络编程
import socket
def main():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 12345))
server_socket.listen(5)
while True:
client_socket, addr = server_socket.accept()
print("Connection from", addr)
data = client_socket.recv(1024)
print("Received", data)
client_socket.sendall(b"Hello from the server!")
client_socket.close()
if __name__ == '__main__':
main()
import socket
def main():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 12345))
client_socket.sendall(b"Hello from the client!")
data = client_socket.recv(1024)
print("Received", data)
client_socket.close()
if __name__ == '__main__':
main()
首先运行服务器代码,然后运行客户端代码。你会看到服务器和客户端之间的消息传递。
import socket
def main():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('localhost', 12345))
while True:
data, addr = server_socket.recvfrom(1024)
print("Received", data, "from", addr)
server_socket.sendto(b"Hello from the server!", addr)
if __name__ == '__main__':
main()
import socket
def main():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.sendto(b"Hello from the client!", ('localhost', 12345))
data, addr = client_socket.recvfrom(1024)
print("Received", data, "from", addr)
if __name__ == '__main__':
main()
首先运行服务器代码,然后运行客户端代码。你会看到服务器和客户端之间的消息传递。
这些示例展示了如何使用Python进行基本的Linux网络编程。你可以根据需要修改这些代码以满足你的需求。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。