socket demo

1.

#!/usr/bin/python3
# 文件名:server.py

# 导入 socket、sys 模块
import socket
import sys

# 创建 socket 对象
serversocket = socket.socket(
            socket.AF_INET, socket.SOCK_STREAM)

# 获取本地主机名                                                                                                                                                                                                                                                                                                                                                       
host = '192.168.15.236'#socket.gethostname()

port = 9999

# 绑定端口号
serversocket.bind((host, port))

# 设置最大连接数,超过后排队
serversocket.listen(5)

while True:
    # 建立客户端连接
    clientsocket,addr = serversocket.accept()      

    print("连接地址: %s" % str(addr))
   
    msg='欢迎访问菜鸟教程!'+ "\r\n"
    clientsocket.send(msg.encode('utf-8'))
    clientsocket.close()

socket demo_第1张图片

2.

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名:client.py
 
import socket               # 导入 socket 模块
 
s = socket.socket()         # 创建 socket 对象
host = '192.168.15.236'#socket.gethostname() # 获取本地主机名
port = 9999                # 设置端口号
 
s.connect((host, port))
print s.recv(1024)
s.close()

/home/r/Pictures/Screenshot from 2023-03-27 10-52-52.png

你可能感兴趣的:(linux,运维,服务器)