__author__ = ‘Administrator‘ import socket # tcp客户端 target_host = ‘www.51cto.com‘ target_port = 80 # AF_INET是标准IPv4地址或主机名,SOCK_STREAM表示是TCP的方式 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立连接 client.connect((target_host, target_port)) # 发送数据 client.send(‘GET / HTTP/1.1\r\n Host:51cto.com\r\n\r\n‘) # 接收返回的数据,参数是buffersize. response = client.recv(4096) print response #UDP客户端 target_host2 = ‘www.51cto.com‘ target_port2 = 80 # 创建套接字,SOCK_DGRAM是UDP的方式。 client2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 无需连接,直接发送。参数:发送内容,发送目标。 client2.sendto(‘GET / HTTP/1.1\r\n Host:51cto.com\r\n\r\n‘, (target_host2, target_port2)) #获取返回数据,远程主机信息和端口号 data, addr = client2.recvfrom(4096) print data
本文出自 “magicpwn” 博客,请务必保留此出处http://magicpwn.blog.51cto.com/10497784/1688584
原文地址:http://magicpwn.blog.51cto.com/10497784/1688584