Python 官方文档:入门教程 => 点击学习
#!/usr/bin/env python# -- encoding: utf-8 --'''@Author : {liush}@License : (C) Copyright 2018-2037, {liush}@Contact
#!/usr/bin/env python
# -- encoding: utf-8 --
'''
@Author : {liush}
@License : (C) Copyright 2018-2037, {liush}
@Contact : {lumia98@vip.qq.com}
@Software: PyCharm
@File : Servers.py
@Time : 2018/9/2 11:28
@Desc : Socket服务端
'''
import socket
ip = '127.0.0.1'
port = 8000
server_socket = socket.socket()
server_socket.bind((ip, port))
server_socket.listen()
while 1:
client_socket, client_addr = server_socket.accept()
print("接收到客户端{}的请求,端口{}".fORMat(client_addr[0], client_addr[1]))
data = client_socket.recv(1024)
if data:
print("----->客服端发来的数据{}".format(data.decode('utf-8')))
file = open('data.txt',mode='a+', encoding='utf-8')
file.write(data.decode('utf-8'))
file.close()
else:
break
print("发送完成")
server_socket.close()
客户端
import socket
ip = '127.0.0.1'
port = 8000
client_socket = socket.socket() #创建socket对象
client_socket.connect((ip,port)) #创建连接
print('正在连接{}服务器,连接端口{}'.format(ip,port))
data = input(">>>>>>>>>>")
client_socket.send(data.encode())
print("连接完成")
client_socket.close()
--结束END--
本文标题: python3 socket实现简单
本文链接: https://www.lsjlt.com/news/192920.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0