zoukankan      html  css  js  c++  java
  • python socket编程


    服务器端


    import socket
    import os

    server = socket.socket()
    server.bind(( '127.0.0.1',9999 ))

    server.listen() #设置最大连接数

    while True:
    conn, addr = server.accept()


    while True:
    data = conn.recv(1024)

    if not data:
    print('客户端已断开')
    break

    cmd_res = os.popen( ( data.decode() ) ).read() #执行系统cmd命令
    cmd_res = cmd_res.encode('utf-8') #执行结果编码

    conn.send( str(len(cmd_res)).encode('utf-8') ) #编码后执行结果内容长度

    conn.recv(1024) #解决linux系统粘包问题

    conn.send(cmd_res)

    server.close()

    客户端

    import socket

    client = socket.socket()
    client.connect(('127.0.0.1',9999))


    while True:
    cmd = input('>>:').scrip()
    if len(cmd) == 0: continue

    client.send(cmd.encode('utf-8')) #发送信息,进行编码

    cmd_res_size = client.recv(1024)
    cmd_res_size = cmd_res_size.decode()
    cmd_res_size = int( cmd_res_size )

    received_size = 0

    client.send('已经准备好接受数据了'.encode('utf-8')) #随便发送数据,解决linux系统粘包问题

    while received_size < cmd_res_size:
    data = client.recv(1024)
    received_size += len(data)

    data = data.decode()

    print(data)

    client.close()

  • 相关阅读:
    spring(三):ApplicationContext
    android Xutils dbutils 注解
    android 录音的断点续传
    android 原生dialog对话框
    android asyncTask 详解
    自定义的dialog
    fragment 添加menu
    android baseApplication 基类
    看项目得到info_freeCsdn-01闪屏页面
    开源项目 github
  • 原文地址:https://www.cnblogs.com/jasonLiu2018/p/10733355.html
Copyright © 2011-2022 走看看