效果:
1 import socket 2 3 4 def main(): 5 while True: 6 # 1.创建tcp套接字 7 tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 9 # 2.连接服务器 10 tcp_socket.connect(('192.168.2.100', 7788)) 11 12 # 3.发送数据 13 send_data = input("请输入要发送的数据:") 14 tcp_socket.send(send_data.encode('gbk')) 15 # 4.关闭套接字 16 tcp_socket.close() 17 if send_data == 'exit': 18 break 19 20 21 if __name__ == '__main__': 22 main()