您好,登錄后才能下訂單哦!
在這個實戰案例中,我們將使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。