Python Socket UDP

1.cmd_server.py

import socket
import subprocess

HOST = '127.0.0.1'
PORT = 9002
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
address = (HOST, PORT)
server.bind(address)

while True:
server_data,client_address = server.recvfrom(4096)
shell = subprocess.Popen(server_data.decode(),shell=True,stdout=subprocess.PIPE)
result = shell.stdout.read()
server.sendto(result,client_address)
server.close()


2.cmd_client.py
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# author:zyjsuper
# datetime:2/1/19 4:38 PM

from socket import *

Host = '127.0.0.1'
Port = 9002
client = socket(AF_INET,SOCK_DGRAM)
client.connect((Host,Port))


while True:
client_data = input("Command>>>:")
client.sendall(bytes(client_data,encoding="utf-8"))
receive_data, server_address = client.recvfrom(4096)
print((receive_data.decode()))


Reference links:
http://blog.51cto.com/youerning/1740646
https://blog.csdn.net/nzjdsds/article/details/82929265
https://www.cnblogs.com/lidam/articles/6931268.html
https://www.jianshu.com/p/2f54dbe481c2
https://blog.csdn.net/qq_31927785/article/details/73197631
https://www.cnblogs.com/greatfish/p/7368433.html
https://www.cnblogs.com/mylovelulu/p/9548570.html
https://www.cnblogs.com/bainianminguo/p/7223168.html
 
posted @ 2019-02-01 18:52  heycomputer  阅读(277)  评论(0编辑  收藏  举报