Skip to content

Instantly share code, notes, and snippets.

@shijiezhou1
Created May 27, 2024 13:53
Show Gist options
  • Save shijiezhou1/c8ea70f2a2cc3b70ff30fc91048f4bf1 to your computer and use it in GitHub Desktop.
Save shijiezhou1/c8ea70f2a2cc3b70ff30fc91048f4bf1 to your computer and use it in GitHub Desktop.

Revisions

  1. shijiezhou1 created this gist May 27, 2024.
    24 changes: 24 additions & 0 deletions client.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import socket

    HOST = '127.0.0.1'
    PORT = 65432

    def send_command(conn, command):
    conn.sendall(command.encode())
    while True:
    data = conn.recv(1024)
    if not data:
    break
    print(data.decode(), end="")
    break

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    print(f"已连接到{HOST}:{PORT}")
    while True:
    command = input("请输入命令(输入'exit'退出):")
    if command.lower() == 'exit':
    break
    send_command(s, command + '\n')
    print("命令执行完毕,请输入下一个命令。")
    print("连接已关闭")