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

套接字练习

时间:2020-04-21 09:27:32      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:执行命令   命令   utf-8   bin   import   套接字   client   sock   net   

# 执行命令的tcp服务端
import socket
import os

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((127.0.0.1, 3000))
server.listen(5)
while 1:
    conn, client_addr = server.accept()
    while 1:
        try:
            dirname = conn.recv(1024).decode(utf-8)
            res = str(os.listdir(dirname)).encode(utf-8)
            conn.send(res)
        except Exception as e:
            print(e)
            break
    conn.close()

 

# 输入命令并显示结果的tcp客户端
import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((127.0.0.1, 3000))
while 1:
    dirname = rE:\python之路\01 课程\week08-day01.encode(utf-8)
    client.send(dirname)
    res = client.recv(1024).decode(utf-8)
    print(res)
    break

 

套接字练习

标签:执行命令   命令   utf-8   bin   import   套接字   client   sock   net   

原文地址:https://www.cnblogs.com/caoyu080202201/p/12742060.html

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