在两台不同的主机使用Python编程完成TCP模式从客户端输入一行字符,发送到服务器后,由服务器转化为大写字母,回送到客户端并显示,并写出代码
时间: 2024-05-15 12:20:06 浏览: 12
好的,我可以回答这个问题。以下是Python代码:
客户端:
```
import socket
HOST = '服务器IP地址'
PORT = 8888
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
data = input('请输入一行字符: ')
client_socket.sendall(data.encode())
received_data = client_socket.recv(1024)
print('服务器回送的大写字母为: ' + received_data.decode())
client_socket.close()
```
服务器:
```
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('服务器已启动,等待客户端连接...')
while True:
client_socket, client_address = server_socket.accept()
print('客户端已连接: ', client_address)
data = client_socket.recv(1024)
print('客户端发送的字符为: ' + data.decode())
upper_data = data.upper()
client_socket.sendall(upper_data)
client_socket.close()
print('客户端已断开连接')
```
注意:需要将客户端代码中的HOST替换为服务器的IP地址。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)