Continuation of the socket and UDP network communications

Examples of remote command execution:

import socket
import subprocess

phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM)

phone.bind(('127.0.0.1',8080))

phone.listen(5)

while 1: # loop connection client
Conn, client_addr = phone.accept ()
Print (client_addr)

while 1:
    try:
        cmd = conn.recv(1024)
        ret = subprocess.Popen(cmd.decode('utf-8'),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        correct_msg = ret.stdout.read()
        error_msg = ret.stderr.read()
        conn.send(correct_msg + error_msg)
    except ConnectionResetError:
        break

conn.close()
phone.close()

import socket

phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # 买 电话

phone.connect (( '127.0.0.1', 8080)) # establish client connection, dial-up

while 1:
cmd = input('>>>')
phone.send(cmd.encode('utf-8'))

from_server_data = phone.recv(1024)

print(from_server_data.decode('gbk'))

phone.close () # on the phone

udp is no link, which end will not be started error

UDP socket communication flow in

  Start talking about the server side. Server initialized Socket, and then receives a message with the Port Binding (bind), recvform, there are two news, message content and other client's address, and then also with the client's address when you receive the reply message, sent back, and finally close the connection ends of an interaction

On the code feel the need to create two files, the file name easily play, for the convenience of watching my two files named udp_server.py (server) and udp_client.py (clients), copy the code server end of the following udp_server.py file to the end of the following cliet udp_client.py copy the code file, and then to run the code udp_server.py file, and then run the code udp_client.py file, then the output window to see below pycharm at the results.

sever-side code sample
Import Socket
udp_sk = socket.socket (type = socket.SOCK_DGRAM) # server creates a socket
udp_sk.bind (( '127.0.0.1', 9000 )) # bind server socket
msg, addr udp_sk.recvfrom = (1024)
Print (MSG)
udp_sk.sendto (b'hi ', addr) # dialogue (receive and transmit)
udp_sk.close () # close server socket

client端代码示例
import socket
ip_port=('127.0.0.1',9000)
udp_sk=socket.socket(type=socket.SOCK_DGRAM)
udp_sk.sendto(b'hello',ip_port)
back_msg,addr=udp_sk.recvfrom(1024)
print(back_msg.decode('utf-8'),addr)

Qq chat similar sample code:

coding:utf-8

Socket Import
for ip_port = ( '127.0.0.1', 8081)
udp_server_sock = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) #DGRAM: Datagram packets meant to symbolize communication protocol UDP
udp_server_sock.bind (ip_port) # port of your external services is this one, all clients are communicating through this port and you

True the while:
qq_msg, addr = udp_server_sock.recvfrom (1024) # blocked state, waiting to receive a message
print ( 'from [% s:% s] of a message: \ 033 [1; 44m% s \ 033 [0m'% ( addr [0], addr [. 1], qq_msg.decode ( 'UTF-. 8')))
back_msg = INPUT ( 'reply message:') .strip ()

udp_server_sock.sendto(back_msg.encode('utf-8'),addr)

import socket
BUFSIZE=1024
udp_client_socket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

qq_name_dic={
'taibai':('127.0.0.1',8081),
'Jedan':('127.0.0.1',8081),
'Jack':('127.0.0.1',8081),
'John':('127.0.0.1',8081),
}

True the while:
qq_name = INPUT ( 'enter chat with:') .strip ()
the while True:
MSG = INPUT ( 'Enter message, sending a carriage return, an input end and his chat q:') .strip ()
IF == MSG 'Q': BREAK
IF MSG or Not Not Not qq_name or qq_name in qq_name_dic: Continue
udp_client_socket.sendto (msg.encode ( 'UTF-. 8'), qq_name_dic [qq_name]) # must with its own address, this UDP is not the same place, without establishing a connection, but to be with his address to the server, or the server can not determine who sent me a message and do not know what to reply to the message, because we are the no connection between the channel

    back_msg,addr=udp_client_socket.recvfrom(BUFSIZE)# 同样也是阻塞状态,等待接收消息
    print('来自[%s:%s]的一条消息:\033[1;44m%s\033[0m' %(addr[0],addr[1],back_msg.decode('utf-8')))

udp_client_socket.close()

Guess you like

Origin www.cnblogs.com/-777/p/11366534.html