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

socket编程

时间:2018-11-27 01:20:56      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:shell   linux服务   recv   end   tin   pen   local   NPU   port   

#linux服务器

import socket
import subprocess
import threading
server = socket.socket()
server.bind((‘‘, 8888))
server.listen(5)
print(等待电话.....)
conn, addr = server.accept()
print(电话来了......)
while True:
    data = conn.recv(10240)
    cmd = subprocess.Popen(data.decode(utf-8),
                           shell=True,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
    stdout = cmd.stdout.read()
    stderr = cmd.stdout.read()
    conn.send(stdout + stderr)

#客户端
import socket
import threading
client = socket.socket()
client.connect((192.168.254.24, 8888))
while True:
    info = input(===>:)
    if not info:continue
    client.send(info.encode(utf-8))
    data = client.recv(10240)
    print(data.decode(utf-8))
#全双工电话
#服务器端
import socket
import subprocess
import threading
server = socket.socket()
server.bind((‘‘, 8888))
server.listen(5)
print(等待电话.....)
conn, addr = server.accept()
print(电话来了......)
def recv():
    while True:
        data = conn.recv(10240)
        print(data.decode(utf-8))
def send():
    while True:
        data = input(===>:)
        conn.send(data.encode(utf-8))
t1 = threading.Thread(target=recv)
t2 = threading.Thread(target=send)
t1.start()
t2.start()



#客户端
import socket
import threading
client = socket.socket()
client.connect((localhost, 8888))
def send():
    while True:
        info = input(===>:)
        client.send(info.encode(utf-8))
def recv():
    while True:
        data = client.recv(1024)
        print(data.decode(utf-8))

t1 = threading.Thread(target=send)
t2 = threading.Thread(target=recv)
t1.start()
t2.start()

 

socket编程

标签:shell   linux服务   recv   end   tin   pen   local   NPU   port   

原文地址:https://www.cnblogs.com/fengzi7314/p/10023683.html

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