Python simple implementation of TCP client

The implementation process is as follows

1. Create a TCP client object;

2. Connect to the server;

3. Realize data transmission and reception through TCP socket;

Implementation code

#! /usr/bin/env python
# __*__coding: UTF-8__*__

import  socket

target_host = "192.168.8.14"
target_port = 9999

#建立一个socket对象
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

#连接服务器
client.connect((target_host,target_port))

'''无限循环实现无限收发数据'''
while True:

    str = input("请输入:")

    client.send(str.encode())

    response = client.recv(4096)

    print ("返回的消息为:",response.decode('gbk'))

Test Results:

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/guo15890025019/article/details/113760016