__author__ = ' Administrator ' Import socket# TCP client Target_host = ' www.51cto.com ' Target_port = 80# af_inet is a standard IPV4 address or host name, Sock_stream represents the way TCP is the client = Socket.socket (socket.af_inet, socket. SOCK_STREAM) # Establish Connection Client.connect ((Target_host, Target_port)) # Send data client.send (' get/http/1.1\r\n host:51cto.com\r\n \ r \ n ') # receives the returned data, parameter is Buffersize.response = CLIENT.RECV (4096) print RESPONSE#UDP client Target_host2 = ' www.51cto.com ' target_ Port2 = 80# Creates a socket, Sock_dgram is the way of UDP. Client2 = Socket.socket (socket.af_inet, socket. SOCK_DGRAM) # No need to connect, send directly. Parameters: Send content, send target. Client2.sendto (' get/http/1.1\r\n host:51cto.com\r\n\r\n ', (Target_host2, Target_port2)) #获取返回数据, remote host information and port number data, Addr = Client2.recvfrom (4096) print data
This article is from the "Magicpwn" blog, make sure to keep this source http://magicpwn.blog.51cto.com/10497784/1688584
Python socket module: TCP,UDP client