socket编程

Posted fengzi7314

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了socket编程相关的知识,希望对你有一定的参考价值。

#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编程的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

使用 Pygments 检测代码片段的编程语言

面向面试编程代码片段之GC

如何在 Django Summernote 中显示编程片段的代码块?

以编程方式将按钮添加到片段