zoukankan      html  css  js  c++  java
  • socket模块

    1.1 server:

    #!/use/local/env python
    # -*- coding:utf-8 -*-

    import socket

    ip_port = ('127.0.0.1', 9999)
    #ip_port = ('0.0.0.0', 9999) #所有IP
    sk = socket.socket()
    sk.bind(ip_port)
    sk.listen(5)

    while True:
    print('server wainting ...')
    conn, add = sk.accept()

    client_data = conn.recv(1024)
    print(str(client_data, encoding='utf-8'))
    conn.sendall(bytes('不要回答,不要回答,不要回答', encoding='utf-8'))
    # conn.sendall(bytes('不要回答,不要回答,不要回答', 'utf-8'))
    # conn.sendall(bytes('不要回答,不要回答,不要回答', 'utf8'))

    conn.close()


    1.2 client:
    #!/use/local/env python
    # -*- coding:utf-8 -*-

    import socket

    ip_port = ('127.0.0.1', 9999)
    sk = socket.socket()
    sk.connect(ip_port)

    sk.sendall(bytes('请求占领地球,请求占领地球,请求占领地球', encoding='utf-8'))
    server_reply = sk.recv(1024)
    print(str(server_reply, encoding='utf-8'))
    sk.close()


    2
    2.1 server:
    #!/use/local/env python
    # -*- coding:utf-8 -*-

    import socket

    ip_port = ('127.0.0.1', 9999)
    #ip_port = ('0.0.0.0', 9999) #所有IP
    sk = socket.socket()
    sk.bind(ip_port)
    sk.listen(5)

    while True:
    print('server wainting ...')
    conn, add = sk.accept()

    client_data = conn.recv(1024) #字符数
    print(str(client_data, encoding='utf-8'))
    conn.sendall(bytes('不要回答,不要回答,不要回答', encoding='utf-8'))
    # conn.sendall(bytes('不要回答,不要回答,不要回答', 'utf-8'))
    # conn.sendall(bytes('不要回答,不要回答,不要回答', 'utf8'))

    while True:
    try:
    client_data2 = conn.recv(1024)
    except Exception:
    break
    conn.send(client_data2)

    conn.close()


    2.2 client:
    #!/use/local/env python
    # -*- coding:utf-8 -*-

    import socket

    ip_port = ('127.0.0.1', 9999)
    sk = socket.socket()
    sk.connect(ip_port)

    sk.sendall(bytes('请求占领地球,请求占领地球,请求占领地球', encoding='utf-8'))
    server_reply = sk.recv(1024)
    print(str(server_reply, encoding='utf-8'))
    while True:
    user_input = input(">>").strip()
    sk.sendall(bytes(user_input, encoding='utf-8'))
    server_reply2 = sk.recv(1024)
    print(str(server_reply2, encoding='utf-8'))

    sk.close()


    3 ssh_socket
    3.1 server
    #!/usr/local/env python
    # -*- coding:utf-8 -*-


    import socket, subprocess

    num = 100

    sk = socket.socket()
    ip_port = ('0.0.0.0', 9999)
    sk.bind(ip_port)
    sk.listen(5)

    count = 0
    while True:
    count += 1
    print("server is waiting [%s]..." %count)
    conn, add = sk.accept()
    #client_data = conn.recv(1024)
    #print(str(client_data, encoding='utf-8'))
    #conn.sendall(bytes('不要回答{3}', encoding='utf-8'))

    while True:
    client_data = conn.recv(num)
    if not client_data:
    print('recv is empty')
    break
    cmd_str = str(client_data, encoding='utf-8')
    cmd_call = subprocess.Popen(cmd_str, shell=True, stdout=subprocess.PIPE)
    cmd_res = cmd_call.stdout.read()
    if len(cmd_res) == 0:
    #if not cmd_res:
    cmd_res = b"has no output! "
    conn.send(cmd_res)
    conn.close()

    3.2 client
    #!/usr/local/env python
    # -*- encoding:utf-8 -*-

    import socket

    num = 100
    sk = socket.socket()
    ip_port = ('10.100.11.211', 9999)
    sk.connect(ip_port)

    while True:
    user_input = input('cmd>').strip()
    if len(user_input) == 0:
    continue
    elif user_input == 'q':
    break
    sk.sendall(bytes(user_input, encoding='utf-8'))

    server_data = sk.recv(num)
    print(str(server_data, encoding='utf-8'), end='')
    while not (len(server_data) < num):
    server_data = sk.recv(num)
    print(str(server_data, encoding='utf-8'), end='')
    sk.close()

    4 ssh传大数据
    4.1 server
    #!/usr/local/env python
    # -*- coding:utf-8 -*-


    import socket, subprocess

    num = 500

    sk = socket.socket()
    ip_port = ('0.0.0.0', 9999)
    sk.bind(ip_port)
    sk.listen(5)

    count = 0
    while True:
    count += 1
    print("server is waiting [%s]..." %count)
    conn, add = sk.accept()
    #client_data = conn.recv(1024)
    #print(str(client_data, encoding='utf-8'))
    #conn.sendall(bytes('不要回答{3}', encoding='utf-8'))

    while True:
    client_data = conn.recv(num)
    if not client_data:
    print('recv is empty')
    break
    cmd_str = str(client_data, encoding='utf-8')
    cmd_call = subprocess.Popen(cmd_str, shell=True, stdout=subprocess.PIPE)
    cmd_res = cmd_call.stdout.read()
    if len(cmd_res) == 0:
    #if not cmd_res:
    cmd_res = b"has no output! "
    CMD_RES_SIZE_MSG = bytes('SEND_DATA_SIZE:%s' %(len(cmd_res)), encoding='utf-8')

    conn.send(CMD_RES_SIZE_MSG)
    conn.recv(50)
    conn.send(cmd_res)
    conn.close()

    4.2 client
    #!/usr/local/env python
    # -*- encoding:utf-8 -*-

    import socket

    num = 500
    sk = socket.socket()
    ip_port = ('10.100.11.211', 9999)
    sk.connect(ip_port)

    while True:
    user_input = input('cmd>').strip()
    if len(user_input) == 0:
    continue
    elif user_input == 'q':
    break
    sk.sendall(bytes(user_input, encoding='utf-8'))

    CMD_SIZE_MSG = sk.recv(50)
    CMD_SIZE_MSG = str(CMD_SIZE_MSG, encoding='utf-8')
    #CMD_RES_SIZE_MSG = 'SEND_DATA_SIZE:%s' %(len(cmd_res))
    CDM_SIZE_GET = CMD_SIZE_MSG.split(':')
    CMD_SIZE = 0
    if CDM_SIZE_GET[0] == 'SEND_DATA_SIZE':
    CMD_SIZE = int(CDM_SIZE_GET[1])
    CMD_ACK = b'ready'
    sk.send(CMD_ACK)

    recv_size = 0
    while recv_size < CMD_SIZE:
    server_data = sk.recv(num)
    recv_size += len(server_data)
    print(str(server_data, encoding='utf-8'), end='')

    #print(str(server_data, encoding='utf-8'), end='')
    #while not (len(server_data) < num):
    # server_data = sk.recv(num)
    # print(str(server_data, encoding='utf-8'), end='')
    sk.close()
  • 相关阅读:
    JMeter Gui – TestElement约定(转自约会言行的博客,链接:http://blog.csdn.net/yue530tomtom/article/details/77649872?locationNum=4&fps=1)
    Maven的下载和安装
    开源蜘蛛集合(转自haizhiguang博客,链接:http://blog.csdn.net/haizhiguang/article/details/20209573)
    HtmlCleaner CleanerProperties 参数配置(转自macken博客,链接:http://macken.iteye.com/blog/1579809)
    java解析xml汇总(转自倾城幻影-Java解析xml汇总,链接:http://www.cnblogs.com/jiugehuanying/archive/2012/01/12/2320058.html)
    [20191005机房测试] Reverse
    【HAOI2010】软件安装
    [20191004机房测试] ZGY的早餐
    [20191004机房测试] C++锦标赛
    [20191004机房测试] 三角
  • 原文地址:https://www.cnblogs.com/linkenpark/p/5252673.html
Copyright © 2011-2022 走看看