码迷,mamicode.com
首页 > 其他好文 > 详细

套接字编程

时间:2019-10-18 19:20:21      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:连接   class   套接字   exception   pytho   RoCE   开始   port   strip()   

目录

程序

#服务器端
# -*-coding:utf-8-*-
# Author: yqliu
# CreatDate: 2019/10/18 14:22

#基础版本1
#*****************************************************************************88

#
# import socket
# print ('基础版本服务端启动。。。。。。。。。。。。。。')
#
# server=socket.socket()
#
# server.bind(((
#     '127.0.0.1',9999
#              )))
#
# server.listen(5)
#
# conn,addr=server.accept()
#
# data=conn.recv(1024).decode('utf-8')
# print (data)
#
#
# send_msg=input('请输入需要传给客户的字符')
# conn.send(send_msg.encode('utf-8'))
# conn.close()
# server.close()

#基础版本2(循环输入套接字)
# print ('基础版本服务端启动。。。。。。。。。。。。。。')
# import socket
#
# server=socket.socket()
#
# server.bind((
#     '127.0.0.1',9527
# ))
#
# server.listen(5)
#
# conn,addr=server.accept()
#
# while True:
#     data=conn.recv(1024).decode('utf-8')
#     if data=='q':
#         break
#     print (data)
#     send_msg=input('请输入发往客户端的字符串').encode('utf-8')
#     conn.send(send_msg)
#
#
#
# conn.close()
#
# server.close()

#基础版本(多个客户端)
# print ('基础版本服务端启动。。。。。。。。。。。。。。')
# import socket
#
# server=socket.socket()
#
# server.bind((
#     '127.0.0.1',9527
# ))
#
# server.listen(5)
#
# while True:
#     conn,addr=server.accept()
#     print (addr)
#     while True:
#         try:
#             data = conn.recv(1024).decode('utf-8')
#             if data=='q':
#                 break
#             print(data)
#             send_msg=input('请输入往客户端发送的字符串')
#             conn.send(send_msg.encode('utf-8'))
#         except Exception as e:
#             print (e)
#             break
#     conn.close()
# server.close()

#基础版本4(作业)
#
# print ('服务端开始启动。。。。。。。。。。。')
#
# import socket
# import subprocess
#
# server=socket.socket()
#
# server.bind((
#     '127.0.0.1',9527
# ))
#
# server.listen(5)
#
# conn,addr=server.accept()
# print (addr)
#
# cmd=conn.recv(1024).decode('utf-8')
#
# obj=subprocess.Popen(
#     cmd,
#     shell=True,
#     stdout=subprocess.PIPE,
#     stderr=subprocess.PIPE
# )
#
# result=obj.stdout.read()+obj.stderr.read()
# conn.send(result)
# conn.close()
# server.close()

#基础版本5(作业升级)

import socket
import subprocess


server=socket.socket()

server.bind((
    '127.0.0.1',9527
))

server.listen(5)

while True:
    conn,addr=server.accept()
    print (addr)
    while True:
        try:
            cmd=conn.recv(1024).decode('utf-8')
            if cmd=='q':
                break
            print (cmd)
            obj=subprocess.Popen(
                cmd,
                shell=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            )
            result=obj.stdout.read()+obj.stderr.read()
            # send_msg=input('请输入需要传给客户端的内容:')
            conn.send(result)
        except Exception as e:
            print (e)
            break
    conn.close()
server.close()
#客户端
# -*-coding:utf-8-*-
# Author: yqliu
# CreatDate: 2019/10/18 14:22

#基础版本1

# import socket
#
# client=socket.socket()
#
# client.connect((
#     '127.0.0.1',9999
# ))
#
# client.send('你好,服务端,我给你发信息了'.encode('utf-8'))
#
# data=client.recv(1024)
# print (data.decode('utf-8'))
# client.close()

#基础版本2(循环输入套接字)

#
# print ('客户端版本已经启动。。。。。。。。。。。。。。。。。。')
# import socket
#
# client=socket.socket()
#
# client.connect((
#     '127.0.0.1',9527
# ))
#
# while True:
#     send_msg=input('请输入需要发往服务端的字符串').strip()
#     if len(send_msg)==0:
#         print ('不能输入空字符串')
#         continue
#     if send_msg=='q':
#         print ('输入q退出程序')
#         break
#     client.send(send_msg.encode('utf-8'))
#     data=client.recv(1024).decode('utf-8')
#     print (data)
#
# client.close()

#基础版本3(多个客户端同时连接服务端)

# import socket
# print ('客户端版本已经启动。。。。。。。。。。。。。。。。。。')
#
# client=socket.socket()
#
# client.connect((
#     '127.0.0.1',9527
# ))
#
# while True:
#     send_msg=input('请输入发往服务端的内容:')
#     if len(send_msg)==0:
#         print ('不能输入空字符')
#         continue
#     if send_msg=='q':
#         break
#     client.send(send_msg.encode('utf-8'))
#     data=client.recv(1024).decode('utf-8')
#     print (data)
#
# client.close()

#基础版本4(作业)
#
# import socket
# client=socket.socket()
#
# client.connect((
#     '127.0.0.1',9527
# ))
#
# cmd=input('请输入往服务端发送命令')
# client.send(cmd.encode('utf-8'))
# data=client.recv(1024).decode('GBK')
# print (data)
# client.close()

#基础版本5(作业升级)

import socket

client=socket.socket()

client.connect((
    '127.0.0.1',9527
))

while True:
    cmd=input('请输入需要传给服务端的命令:')

    if cmd=='q':
        break
    if len(cmd)==0:
        print ('不能输入空字符')
        continue

    client.send(cmd.encode('utf-8'))
    data=client.recv(1024).decode('gbk')
    print (data)
client.close()

套接字编程

标签:连接   class   套接字   exception   pytho   RoCE   开始   port   strip()   

原文地址:https://www.cnblogs.com/ztzdhbg/p/11699831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!