Example one,
server:
#socket socket (IP + port number) (qq, wechat message transmission relies on the socket module), CS architecture
Import socket
server socket.socket = ()
# instantiates a socket object, the default tcp / ip protocol
server .bind (( "127.0.0.1", 8888))
# bind client, VIP, tuples form, ( '') is equivalent to ( '0.0.0.0')
# declared the end of the visit the client address and port number
server.listen (. 5)
# semijoin pool, listen (5) the maximum number of connections
Print ( 'the wait Telephone ..........')
() Conn, addr = server.accept
# listen for client connecting
# operation produces two return value: connection information, client information
Print ( '............ Telephone Call')
Data = conn.recv (10240)
# to the received information. 10240: Maximum acceptable bytes just write '
Print (data.decode (' UTF-. 8 '))
client:
importsocket
client=socket.socket()
client.connect(('127.0.0.1',8888))
cmd=input('==>:')
client.send(cmd.encode('utf-8'))
result:
Example 2,
Using the subprocess module and socket module executes the command cmd in other hosts:
server: import socket,subprocess server = socket.socket() server.bind(('127.0.0.1',8888)) server.listen(5) print('wait telephone..........') conn,addr = server.accept() print('call telephone............') while True: cmd = conn.recv(10240) process = subprocess.Popen(cmd.decode('utf-8'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) stdout=process.stdout.read() stderr=process.stderr.read() result = stdout+stderr conn.send(result) client: import socket client = socket.socket() client.connect(('127.0.0.1',8888)) while True: cmd = input('==>:').strip() if cmd: client.send(cmd.encode('utf-8')) else: continue result = client.recv(10240) print(result.decode('gbk'))
result:
Example 3,
Simple chat software (first two machines to establish communication using the socket, and then make use of multi-threading to send and receive messages while running.)
server: import socket,threading server = socket.socket() server.bind(('127.0.0.1',8888)) server.listen(5) print('wait telephone..........') conn,addr = server.accept() print('call telephone............') def recv(): while True: data = conn.recv(1024) print(data.decode('utf-8')) def send(): while True: cmd = input('==>:') conn.send(cmd.encode('utf-8')) t1 = threading.Thread(target=recv) t2 = threading.Thread(target=send) t1.start() t2.start() client: import socket,threading client = socket.socket() client.connect(('127.0.0.1',8888)) def send(): while True: cmd = input('==>:').strip() client.send(cmd.encode('utf-8')) def recv(): while True: data = client.recv(1024) print(data.decode('gbk')) t1 = threading.Thread(target=recv) t2 = threading.Thread(target=send) t1.start() t2.start()
result:
Example 4,
Check the IP address and port number through the barrier:
Socket Import the hosts = [ '192.168.254.12:80','192.168.254.12:3306','2.2.2.2:80','3.3.3.3:80'] socket.setdefaulttimeout (. 1) for the hosts in Host: IP = host.split ( ':') [0] Port = host.split ( ':') [. 1] Server socket.socket = () res = server.connect_ex ((IP, int (Port))) # if res is 0 represents ok, the connection is not 0 for failure : IF RES == 0 Print ( '% S iS OK'% IP) the else: Print ( '% S iS not OK'% IP) Import Socket socket.setdefaulttimeout (. 1) # timeout host_list = [ '192.168.4.145:5555','192.168.4.146:555','192.168.4.147:5555','192.168.31.199:445'] for host_list info in: server = socket.socket() re.compile = ip ( '(*):.?. (*)'). Search (info) .group (1) Port = re.compile ( '(*):.?. (*)'). Search (info) .group (2) RES = server.connect_ex ((IP, int (port))) IF RES == 0: Print ( '% S -% S ports normal'% (IP, port)) the else: print ( '% s -% s port exception'% (ip, port))