用一台客户端电脑发送一串小写字符串,另外一台服务端电脑接受字符串并转为大写字母并传回给客户端电脑,用python写这个过程的套接字编程的客户端和服务端程序
时间: 2024-05-12 21:20:31 浏览: 120
流式套接字实现简单的客户端服务端通信过程
5星 · 资源好评率100%
客户端代码:
```python
import socket
HOST = 'localhost'
PORT = 8888
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
message = input("请输入你要发送的字符串:")
client_socket.sendall(message.encode())
data = client_socket.recv(1024)
print('接收到的数据:', data.decode())
client_socket.close()
```
服务端代码:
```python
import socket
HOST = ''
PORT = 8888
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen(1)
print('等待客户端连接中...')
conn, addr = server_socket.accept()
print('已连接:', addr)
data = conn.recv(1024)
print('接收到的数据:', data.decode())
message = data.decode().upper()
conn.sendall(message.encode())
conn.close()
server_socket.close()
```
注意:客户端和服务端的IP地址和端口号需要一致。
阅读全文