Few words said,
to get down to business directly
A: client a total is divided into five areas:
1. Create client socket
2. And the server socket connection is established
3. Send data
4. Receive send
5. Close the client socket
The introduction of two: the socket class
Import the socket module
The import socket
Create a client socket object
Socket. The socket (AddressFamily, Type)
The send (data) to send data, the data is binary data
Recv (buffersize) receiving data, buffersize is the length of each receive data
3: application development
The import socket
Def the main () :
# to create TCP client socket
Tcp_client_socket=socket. The socket (socket. AF_INET, socket. SOCK_STREAM)
# and server applications to establish connection
Tcp_client_socket. Connect ((6666) "192.168.28.1)
# ready to send data
Send_data="https://bbs.csdn.net/topics/: hello, hello, is that the server!" Encode (" GBK ")
# to send data
Tcp_client_socket. Send (send_data)
# receives the data, the maximum number of bytes received data is 1024
Recv_data=https://bbs.csdn.net/topics/tcp_client_socket.recv (1024)
# returned directly is the server program to send binary data
Print (recv_data)
# to decrypt data
Recv_content=recv_data. Decode (" GBK ")
Print (" receiving data from the server: ", recv_content)
# to close the socket
Tcp_client_socket. Close ()
If __name__=="__main__ ':
The main ()
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
If viewers don't understand, I give you an example:
Create a socket is equivalent to buy mobile phone
And the server connection equivalent to whom you give phone call to
Send the data of your call and want to talk to each other to say
Receiving data is equivalent to what you said received each other
Close the client is equivalent to hung up the phone, hang up the phone
Said it is good to understand?
* * * * * attention * * * * *
Windows of GBK code format is
MAC OS coding format is utf-8
Parameter 1: 'AF_INET', said the IPv4 address type
Parameter 2: type SOCK_STREAM, said the TCP transport protocol
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The first blog, blog also not too will use, if you like them, will not update the blog regularly,
Hope bosses under the mouth, the younger brother is a little white, please leave a message what improvements;
thank you!