优化并改编以下代码,使其和原来有部分出入但实现效果相同: 1. import socket 2. 3. 4. def receive(): 5. # 创建套接字 6. udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 7. 8. # 准备数据9. file_name = input("Please input the save file name:") 10. 11. # 发送数据 12. ip = input("Please input the sender's ipv4 address:") 13. udp_socket.sendto(file_name.encode('gbk'), (ip, 7788)) 14. 15. # 接收数据 16. recv_data = udp_socket.recvfrom(1024) 17. file_data = recv_data[0] 18. with open(file_name, 'wb') as f: 19. f.write(file_data) 20. print("Receive successfully!") 21. # 关闭套接字 22. udp_socket.close() 23. 24. 25.def send(): 26. # 创建套接字 27. udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 28. 29. # 绑定本地信息 30. localaddr = ('', 7788) 31. udp_socket.bind(localaddr) 32. 33. # 接收数据 34. while True: 35. recv_data = udp_socket.recvfrom(1024) 36. recv_msg = recv_data[0] 37. send_addr = recv_data[1] 38. print("%s:%s" % (str(send_addr), recv_msg.decode('gbk'))) 39. 40. # 读取文件并传输文件 41. with open(recv_msg.decode('gbk'), 'rb') as f: 42. file_data = f.read() 43. udp_socket.sendto(file_data, send_addr) 44. 45. print("Send successfully!") 46. break 47. 48. # 关闭套接字 49. udp_socket.close() 50. 51. 52.if name == 'main': 3553. while True: 54. answer = input("This is a simple program relying on the Udp protocol, \nif you want to send the file," 55. "please input 1,\n if you want to receive th e file, please input 2, \n if you want exit, " 56. "please input 0:\n") 57. if answer == '0': 58. break 59. if answer == '1': 60. send() 61. if answer == '2': 62. receive()
时间: 2023-12-03 20:46:41 浏览: 120
Python程序设计:python模拟三次握手.pptx
以下是优化并改编后的代码,改动主要是将 receive() 和 send() 函数中的 input() 改为函数参数接收:
```
import socket
def receive(file_name, ip):
# 创建套接字
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 发送数据
udp_socket.sendto(file_name.encode('gbk'), (ip, 7788))
# 接收数据
recv_data = udp_socket.recvfrom(1024)
file_data = recv_data[0]
with open(file_name, 'wb') as f:
f.write(file_data)
print("Receive successfully!")
# 关闭套接字
udp_socket.close()
def send(file_path):
# 创建套接字
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 绑定本地信息
localaddr = ('', 7788)
udp_socket.bind(localaddr)
# 接收数据
while True:
recv_data = udp_socket.recvfrom(1024)
recv_msg = recv_data[0]
send_addr = recv_data[1]
print("%s:%s" % (str(send_addr), recv_msg.decode('gbk')))
# 读取文件并传输文件
with open(file_path, 'rb') as f:
file_data = f.read()
udp_socket.sendto(file_data, send_addr)
print("Send successfully!")
break
# 关闭套接字
udp_socket.close()
if __name__ == '__main__':
while True:
answer = input("This is a simple program relying on the Udp protocol, \nif you want to send the file, please input 1,\nif you want to receive the file, please input 2, \nif you want exit, please input 0:\n")
if answer == '0':
break
if answer == '1':
file_path = input("Please input the file path to send:")
send(file_path)
if answer == '2':
file_name = input("Please input the save file name:")
ip = input("Please input the sender's ipv4 address:")
receive(file_name, ip)
```
改编后的代码中,receive() 和 send() 函数分别接收一个参数,即文件名或文件路径,而不是通过 input() 函数从命令行读取。这样可以使代码更加模块化和可重用。同时,代码中的注释也进行了修改和补充,以提高代码可读性和易懂性。
阅读全文