I've been watching it lately 《Python Black hat 》 Because this book was written seven or eight years ago , So the program in the book , use Python 3.x compile , Enough to run
Therefore, it is suggested that Path Change to Python2.7 Of , I am using Wing pro 8IDE.
Change to default 2.7, You can run the program in the book .
because TCP Sockets are connection oriented , So it's also called stream based (stream) Socket .TCP yes Transmission Control Protocol( Transmission control protocol ) Abbreviation , Meaning for “ Control of the data transmission process ”.
ordinary TCP client
#python2.7.18
import socket
target_host = "www.baidu.com"
target_port=80
# Build a socket object
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Connect to the client
client.connect((target_host,target_port))
# Send some data
client.send("GET / HTTP/1.1\r\nHost:baidu.com\r\n\r\n")
# Receive some data ,4096 Is the buffer size
response=client.recv(4096)
print response
Remember first ... I haven't learned network programming either
socket.socket([family[, type[, proto]]])
- family: Socket family can make AF_UNIX perhaps AF_INET.
- type: Socket types can be classified into connection oriented or connectionless
SOCK_STREAM
(TCP) orSOCK_DGRAM(UDP)
.- protocol: Generally speaking, we don't think that 0.
I think this TCP Client means , Is to be able to actively apply for and TCP Server establishes connection ...maybe
This socket Two parameters of the object , first AF_INET It's using standard ipv4 Address or host name (AF_INET6 The use of ipv6 Address or host name ). the second SOCK_STREAM, This is connection oriented , That is to say, this is a TCP client .
import socket
target_host = "127.0.0.1"
target_port=80
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
# Send the data to the server you want to send it to
client.sendto("AAAAAAA",(target_host,target_port))
# Received the returned data , And the information and port number of the remote host
data,addr=client.recvfrom(4096)
print data;
Perform before , We're going to turn on 80 Listening mode of the port
nc -uvlp(u:udp,v: Show the running process ,l: Use monitor mode , Supervise incoming data ,p: Set communication port )
nc received . however nc No message back to us
We need to put our own TCP The server is bound to the command line shell Or create a proxy ( These two requirements will be completed later ). First we create a standard multithreading TCP The server .
#-*- coding:utf8 -*-
import socket
import threading
bind_ip = "0.0.0.0" # binding ip: This represents any ip Address
bind_port = 8888
# A socket used only to listen and accept connection requests from clients
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Determine what the server needs to listen for IP Address and port
server.bind((bind_ip, bind_port))
# The maximum number of connections is 5
server.listen(5)
print "[*] Listening on %s:%d" % (bind_ip, bind_port)
# This is the customer processing process
def handle_client(client_socket):
# Print out the content sent by the client
request = client_socket.recv(1024)
print "[*] Received: %s" % request
# Packets sent to the client
client_socket.send("ACK!")
# Close socket
client_socket.close()
while True:
#client It is the socket from the client to the server , differ sever Socket , The server communicates with the client through sending and receiving data on the new socket .addr It's a tuple ,(" Address ", Port number )
client,addr = server.accept()
print "[*] Accepted connection from: %s:%d" % (addr[0], addr[1])
# Suspend client thread , Processing transmission ru The data of
client_handler = threading.Thread(target=handle_client, args=(client,))
client_handler.start()
Python Network programming is very important to socket accept The understanding of function - The little girl's test road - Blog Garden (cnblogs.com)
Python Multithreading | Novice tutorial (runoob.com)
client_handler = threading.Thread(target=handle_client, args=(client,))
This line of code means , Create thread , And call handle_client function , The parameters come from args=(), Namely client. At the same time, it should be noted that ,args= It must be followed by a tuple , That's why (client,) This way .
Threads .start() Is to start the thread
of threading.Thread() The definition of
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None):
"""This constructor should always be called with keyword arguments. Arguments are:
*group* should be None; reserved for future extension when a ThreadGroup
class is implemented.
*target* is the callable object to be invoked by the run()
method. Defaults to None, meaning nothing is called.
*name* is the thread name. By default, a unique name is constructed of
the form "Thread-N" where N is a small decimal number.
*args* is the argument tuple for the target invocation. Defaults to ().
*kwargs* is a dictionary of keyword arguments for the target
invocation. Defaults to {}.
If a subclass overrides the constructor, it must make sure to invoke
the base class constructor (Thread.__init__()) before doing anything
else to the thread.
"""