利用socket传递参数,把运行的程序与客户端结合,先启动服务端,再启动客户端。基础代码如下:
参考:https://blog.csdn.net/qq_38641985/article/details/84975910
服务端代码
import socket
import time
print("服务端开启")
#创建套接字
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#设置IP和端口
#host = socket.gethostname()
host = '127.0.1.1'
port = 3333
#bind绑定该端口
mySocket.bind((host, port))
#监听
mySocket.listen(10)
while True:
#接收客户端连接
print("等待连接....")
client, address = mySocket.accept()
print("新连接")
print("IP is %s" % address[0])
print("port is %d\n" % address[1])
while True:
#发送消息
msg = input("服务端发送:")
client.send(msg.encode(encoding='utf-8'))
print("发送完成")
print (time.strftime('%Y-%m-%d %H:%M:%S&#