import socket
ip = '192.168.3.1'
def user_input():
message = input('>>>>')
if message == '拜拜':
send_message(message)
return False
elif message == '发送图片':
send_pic()
else:
send_message(message)
return True
def send_message(message):
conversation, address = server.accept()
conversation.send(message.encode())
conversation.close()
def send_pic():
conversation, address = server.accept()
pic = input('图片路径:')
print(pic)
with open('./蕾姆酱.jpg', 'rb') as f:
pic = f.read()
conversation.send(pic)
conversation.close()
def get_message():
conversation, address = server.accept()
data = conversation.recv(1024)
message = data.decode(encoding='utf-8')
if message == '发送图片':
get_pic()
else:
print(message)
def get_pic():
conversation, address = server.accept()
data = conversation.recv(1024)
pic = bytes()
while data:
pic += data
data = conversation.recv(1024)
with open('./pic.jpg', 'wb') as f:
f.write(pic)
if __name__ == '__main__':
# 1.创建对象
server = socket.socket()
# 2.绑定地址
server.bind(('192.168.3.1', 8080))
# 3.监听
server.listen(50)
while True:
if user_input():
pass
else:
break
get_message()
import socket
ip = '192.168.3.1'
def user_input():
message = input('>>>>')
if message == '拜拜':
send_message(message)
return False
elif message == '发送图片':
send_pic()
else:
send_message(message)
return True
def send_message(message):
client.connect((ip, 8080))
client.send(message.encode())
client.close()
def send_pic():
client.connect((ip, 8080))
pic = input('图片路径:')
print(pic)
with open('./蕾姆酱.jpg', 'rb') as f:
pic = f.read()
client.send(pic)
client.close()
def get_message():
client.connect((ip, 8080))
data = client.recv(1024)
message = data.decode(encoding='utf-8')
if message == '发送图片':
get_pic()
else:
print(message)
def get_pic():
client.connect((ip, 8080))
data = client.recv(1024)
pic = bytes()
while data:
pic += data
data = client.recv(1024)
with open('./pic.jpg', 'wb') as f:
f.write(pic)
if __name__ == '__main__':
# 1.创建套接字
client = socket.socket()
# 2.连接服务器
# client.connect((ip, 8080))
while True:
get_message()
if user_input():
pass
else:
break
网友评论