程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python TCP client based on socket

編輯:Python
'''
be based on socket Realized TCP client
'''
import socket
# establish socket object
# Parameter 1 indicates IP Address type (AF_INET by IPV4,AF_INET6 by IPV6), Parameter 2 indicates the type of connection (SOCK_STREAM Express TCP form ,SOCK_DGRAM Express UDP form )
client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM) # representative (ipv4,TCP)
# Connect to server ( Fill target in tuple ip Address and port number )
client_socket.connect(('127.0.0.1',7777))
# Prepare the data , Need to convert to binary data ,encode() The local string encoding format is filled in ,mac、linux fill utf-8
data='hello'.encode('gbk')
# Send data to the server
client_socket.send(data)
# receive data , You must specify the size of the received data , Unit byte , Maximum 4096, namely 4k
recv_data=client_socket.recv(1024)
# The received data shall be processed decode() decode , Fill in whatever code you use when sending
recv_data=recv_data.decode('gbk')
print(recv_data)
# Close the connection
client_socket.close()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved