current position:Home>Python socket implements TCP server and client
Python socket implements TCP server and client
2022-01-29 14:30:13 【Xing Chuxin】
- Python : 3.8.11
- OS : Ubuntu Kylin 20.04
- Conda : 4.10.1
- Pycharm : 2021.1.3
Code example
tcp_server
""" @Author : First intention @Date : 10/12/21 7:11 PM """
import socket
import threading
def client_handle(client):
message = client.recv(1024)
print("message", message)
client.send("server accept!".encode("utf-8"))
client.close()
bind_ip = "127.0.0.1"
bind_port = 7778
# socket.AF_INET: Network type ,IPv4
# socket.SOCK_STREAM: TCP
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# SOL_SOCKET: In use socket
# socket.SO_REUSEADDR,1 : More meaning and function , For restart socket_server It can still go smoothly bind
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((bind_ip, bind_port))
# Number of monitors 3
server.listen(3)
while True:
client, address = server.accept()
print("client", client)
print("address", address)
client_thread = threading.Thread(target=client_handle, args=(client,))
client_thread.start()
# This code is unreachable
server.close()
Copy code
tcp_client
""" @Author : First intention @Date : 10/12/21 7:27 PM """
import socket
server_ip = "127.0.0.1"
server_port = 7778
# socket.AF_INET: Network type ,IPv4
# socket.SOCK_STREAM: TCP client
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((server_ip, server_port))
# TypeError: a bytes-like object is required, not 'str'
# client.send("hello world")
client.send("hello world".encode("utf-8"))
response = client.recv(1024)
print(response)
client.close()
Copy code
Running effect
tcp_server
- Use IDE Forcibly ended tcp_server, So there will be KeyboardInterrupt
/home/coder/anaconda3/envs/py38/bin/python /home/coder/PycharmProjects/pythonProject1/tcp_server.py
client <socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 7778), raddr=('127.0.0.1', 45876)>
address ('127.0.0.1', 45876)
message b'hello world'
Traceback (most recent call last):
File "/home/coder/PycharmProjects/pythonProject1/tcp_server.py", line 33, in <module>
client, address = server.accept()
File "/home/coder/anaconda3/envs/py38/lib/python3.8/socket.py", line 292, in accept
fd, addr = self._accept()
KeyboardInterrupt
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
Copy code
tcp_client
/home/coder/anaconda3/envs/py38/bin/python /home/coder/PycharmProjects/pythonProject1/tcp_client.py
b'server accept!'
Process finished with exit code 0
Copy code
Learning recommendations
- Python file - English
- Python file - chinese
- Python standard PEP
- Python standard google edition
- Python Source code
- Python PEP
- You Qilin
- The nuggets platform
- gitee platform
Python Open source 、 Cross platform 、 interpreted 、 Interactive and so on , Worth learning .
Python Philosophy of design : grace , clear , Simple . Advocate a way , It's better to have only one way to do one thing .
The code should be written according to the standard , It helps to communicate and understand .
Every language has a unique idea , Beginners need to change their thinking 、 Practice steadfastly 、 Keep accumulating .
copyright notice
author[Xing Chuxin],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201291430121375.html