import socket
def target_tcp(host,port):
""" 建立tcp连接 """
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建socket对象
client.connect((host,port)) # 建立连接
client.send(b'Hello World') # 发送数据
data = client.recv(1024) # 接收数据
print(data.decode('utf-8')) # 打印数据
client.close() # 关闭连接
def target_udp(host,port):
""" 建立udp连接 """
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 创建udp连接
s.sendto(b'Hello World',(host,port)) # 发送数据
data,addr = s.recvfrom(1024) # 接收数据
print(data.decode('utf-8')) # 打印数据
s.close() # 关闭连接