29 lines
507 B
Python
29 lines
507 B
Python
# 导入 socket、sys 模块
|
|
import socket
|
|
import sys
|
|
|
|
# 创建 socket 对象
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
# 获取本地主机名
|
|
host = socket.gethostname()
|
|
|
|
# 设置端口号
|
|
port = 9999
|
|
|
|
# 连接服务,指定主机和端口
|
|
s.connect((host, port))
|
|
|
|
# 接收小于 1024 字节的数据
|
|
|
|
tex = "this is test"
|
|
s.send(tex.encode('utf-8'))
|
|
msg1 = s.recv(1024)
|
|
print (msg1.decode('utf-8'))
|
|
s.send(tex.encode('utf-8'))
|
|
msg2 = s.recv(1024)
|
|
print (msg2.decode('utf-8'))
|
|
s.close()
|
|
|
|
|