文章目录
import socket
from threading import Thread
from multiprocessing import Process
"""
服务端:
1、要有固定的ip+port
2、24小时不间断服务
3、能够支持并发
"""
server = socket.socket()
server.bind(('127.0.0.1', 8080))
server.listen(5)
while True:
conn, addr = server.accept()
while True:
try:
data = conn.recv(1024)
print(data.decode('utf-8'))
conn.send(data.upper())
except ConnectionError as e:
print(e)
break
conn.close()
import socket
client = socket.socket()
client.connect(('127.0.0.1', 8080))
while True:
client.send(b'hello zpp')
data = client.recv(1024)
print(data.decode('utf-8'))
import socket
from threading import Thread
from multiprocessing import Process
"""
服务端:
1、要有固定的ip+port
2、24小时不间断服务
3、能够支持并发
"""
server = socket.socket()
server.bind(('127.0.0.1', 8080))
server.listen(5)
def talk(conn):
while True:
try:
data = conn.recv(1024)
if len(data) == 0:
break
print(data.decode('utf-8'))
conn.send(data.upper())
except ConnectionError as e:
print(e)
break
conn.close()
while True:
conn, addr = server.accept()
t = Thread(target=talk, args=(conn,))
t.start()
- 客户端不变, 如果pycharm不支持一个py文件运行多次
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b1a250401bab867ab4ea375b571702c3.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ff0919f5b1be05afb787cf3e8acb7dd5.png)
相关连接(笔记来自于视频课程的归类整理):
[1]: https://www.bilibili.com/video/BV1QE41147hU?p=17
[2]: https://www.cnblogs.com/Dominic-Ji/p/11093137.html