tcp.md 4.6 KB
Newer Older
F
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
# Python 网络编程

编写一个简易的 ECHO 服务机器人。满足:

1. 客户端发送文本,服务端回复同样的文本,两边都打印各自收到的文本。
2. 客户端支持用户输入'q' 退出。
3. 客户端支持每次不大于 140 的文本输入,如果超出则提示重新输入。
4. 客户端必须正确打印字符串。

```python
# -*- coding: UTF-8 -*-
import socket
import sys

def echo_server(host, port):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((host, port))
        s.listen()
        conn, addr = s.accept()
        with conn:
            print('收到客户端请求:', addr)
            while True:
                data = conn.recv(1024)
                print('收到客户端数据:', str(data, 'utf8'))
                if not data:
                    break
                conn.sendall(data)

def echo_client(host, port, input_message):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((host, port))

        # TODO(You): 请正确实现发送处理逻辑
        while True:
            msg = input_message("请输入信息[按q退出]:")
            data = ...
            print('收到服务端回包:', str(data, 'utf8'))

if __name__ == '__main__':
    host = '127.0.0.1'
    port = 9999

    # 如果命令行指定了 'server' 参数,就启动服务端程序
    # 否则,启动客户端程序
    if len(sys.argv) > 1 and sys.argv[1] == 'server':
        echo_server(host, port)
    else:
        echo_client(host, port, lambda tip: input(tip))
```

请选出下列能**正确**实现客户端处理用户输入,发送消息的代码。

## template

```python
import socket
import sys


def echo_server(host, port):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((host, port))
        s.listen()
        conn, addr = s.accept()
        with conn:
            print('收到客户端请求:', addr)
            while True:
                data = conn.recv(1024)
                print('收到客户端数据:', str(data, 'utf8'))
                if not data:
                    break
                conn.sendall(data)


def echo_client(host, port, input_message):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((host, port))
        while True:
            msg = input_message("请输入信息[按q退出]:")
            if len(msg) > 140:
                print("您的输入太长了~~,请重新输入!")
                continue

            if msg == 'q':
                break

            msg_buffer = bytes(msg, 'utf-8')
            s.sendall(msg_buffer)

            data = s.recv(1024)
            print('收到服务端回包:', str(data, 'utf8'))

if __name__ == '__main__':
    host = '127.0.0.1'
    port = 9999
    if len(sys.argv) > 1 and sys.argv[1] == 'server':
        echo_server(host, port)
    else:
        echo_client(host, port, lambda tip: input(tip))
```

## 答案

```python
while True:
    msg = input_message("请输入信息[按q退出]:")
    if msg == 'q':
        break

    if len(msg) > 140:
        print("您的输入太长了~~,请重新输入!")
        continue

    msg_buffer = bytes(msg, 'utf-8')
    s.sendall(msg_buffer)

    data = s.recv(1024)
    print('收到服务端回包:', str(data, 'utf8'))
```

## 选项

### A

```python
while True:
    msg = input_message("请输入信息[按q退出]:")
    if msg == 'q':
        break

    msg_buffer = bytes(msg, 'utf-8')
    s.sendall(msg_buffer)

    data = s.recv(1024)
    print('收到服务端回包:', str(data, 'utf8'))
```

### B

```python
while True:
    msg = input_message("请输入信息[按q退出]:")
    if msg == 'q':
        break

    if len(msg) > 140:
        print("您的输入太长了~~,请重新输入!")
        continue

    s.sendall(msg)
    data = s.recv(1024)

    print('收到服务端回包:', str(data, 'utf8'))
```

### C

```python
while True:
    msg = input_message("请输入信息[按q退出]:")
    if msg == 'q':
        break

    if len(msg) > 140:
        print("您的输入太长了~~,请重新输入!")
        continue

    msg_buffer = bytes(msg, 'utf-8')
    s.sendall(msg_buffer)
    data = s.recv()

    print('收到服务端回包:', str(data, 'utf8'))
```

### D

```python
while True:
    msg = input_message("请输入信息[按q退出]:")
    if msg == 'q':
        break

    if len(msg) > 140:
        print("您的输入太长了~~,请重新输入!")
        continue

    msg_buffer = bytes(msg, 'utf-8')
    s.sendall(msg_buffer)

    data = s.recv(1024)
    print('收到服务端回包:', data)
```