标签:循环 访问 loop list soc read prot 建立 分享图片
socket.socket(family[,type[,protocal]])
参数
#coding:utf-8
#创建TCP服务端
import socket
import threading
import time
def dealClient(sock, addr):
#第四步:接收传来的数据,并发送给对方数据
print(‘Accept new connection from %s:%s...‘ % addr)
sock.send(b‘Hello,I am server!‘)
while True:
data = sock.recv(1024)
time.sleep(1)
if not data or data.decode(‘utf-8‘) == ‘exit‘:
break
print ‘-->>%s!‘ % data.decode(‘utf-8‘)
sock.send((‘Loop_Msg: %s!‘ % data.decode(‘utf-8‘)).encode(‘utf-8‘))
#第五步:关闭套接字
sock.close()
print(‘Connection from %s:%s closed.‘ % addr)
if __name__=="__main__":
#第一步:创建一个基于IPv4和TCP协议的Socket
# 套接字绑定的IP(127.0.0.1为本机ip)与端口
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((‘127.0.0.1‘, 9999))
#第二步:监听连接
s.listen(5)
print(‘Waiting for connection...‘)
while True:
# 第三步:接受一个新连接:
sock, addr = s.accept()
# 创建新线程来处理TCP连接:
t = threading.Thread(target=dealClient, args=(sock, addr))
t.start()
#coding:utf-8
import socket
#初始化Socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#连接目标的ip和端口
s.connect((‘127.0.0.1‘, 9999))
# 接收消息
print(‘-->>‘+s.recv(1024).decode(‘utf-8‘))
# 发送消息
s.send(b‘Hello,I am a client‘)
print(‘-->>‘+s.recv(1024).decode(‘utf-8‘))
s.send(b‘exit‘)
#关闭套接字
s.close()
服务端:
C:\Python27\python.exe F:/python_scrapy/python_study/TCP_client.py
-->>Hello,I am server!
-->>Loop_Msg: Hello,I am a client!
Process finished with exit code 0
通信双方以流的形式发送数据
#coding:utf-8
import socket
#创建Socket,绑定指定的ip和端口
#SOCK_DGRAM指定了这个Socket的类型是UDP。绑定端口和TCP一样。
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((‘127.0.0.1‘, 9999))
print(‘Bind UDP on 9999...‘)
while True:
# 直接发送数据和接收数据
data, addr = s.recvfrom(1024)
print(‘Received from %s:%s.‘ % addr)
s.sendto(b‘Hello, %s!‘ % data, addr)
- 客户端:
#coding:utf-8
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for data in [b‘Hello‘, b‘World‘]:
# 发送数据:
s.sendto(data, (‘127.0.0.1‘, 9999))
# 接收数据:
print(s.recv(1024).decode(‘utf-8‘))
s.close()
标签:循环 访问 loop list soc read prot 建立 分享图片
原文地址:https://www.cnblogs.com/guguobao/p/9400534.html