经过无数次测试,失败,失败,失败,和华为技术沟通,失败,失败,失败,上网查询,没有;我都有些心灰意冷,最终解决,分享给大家。
不是替华为打广告,服务应该是没得说,有时一个问题的解决他是一个团队来处理的,再次说声谢谢!不啰嗦下面是正文。
1.运行环境
python软件用conda安装。python3.6.5;打包pyinstaller3.4测试平台用pycharm;本地window7;[华为云](https://l.gushuji.site/huawei)服务器ecs widondws2008
2.1.客户机程序(不用打包直接本地运行)
#!/usr/bin/python# -*- coding: UTF-8 -*-# 文件名:client.py import socket # 导入 socket 模块 # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 创建 socket 对象# host = socket.gethostname() # 获取本地主机名# port = 12345 # 设置端口号# print('客户端名称=',host,'客户端端口=',port)## client_send_data='客户端发送字符:client send '.encode('utf-8')# client_receive_data=''## s.connect((host, port))# print('客户端开始发送数据...')# s.send(client_send_data)# print('客户端接收数据=', s.recv(1024).decode('utf-8'))# s.close() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect(("114.116.48.152", 12345))print(s.recv(1024).decode()) data = "client"while True: if data:print(s.recv(1024).decode()) data = input("Please input your name: ") if not data:continue s.send(data.encode()) print(s.recv(1024).decode()) if data == "exit":break s.close()
2.2.服务器程序
#!/usr/bin/python# -*- coding: UTF-8 -*-# 文件名:server.py# import opimport socket # 导入 socket 模块 # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建 socket 对象# host = socket.gethostname() # 获取本地主机名# port = 12345 # 设置端口# s.bind((host, port)) # 绑定端口## s.listen(5) # 等待客户端连接# print('服务器主机名=',host,'服务器端口=',port)## server_send_data='服务器发送字符:server send '.encode('utf-8')# server_receive_data=''## while True:# c, addr = s.accept() # 建立客户端连接。# print( '连接地址:', addr)# print('服务器开始发送数据...')# c.send(server_send_data)# server_receive_data=c.recv(1024).decode('utf-8')# print('服务器接收数据=',server_receive_data)# c.close() # 关闭连接 import socketimport threading,time def tcplink(conn, addr): print("Accept new connection from %s:%s" % addr) conn.send(b"Welcome!\n") while True: conn.send(b"What's your name?") data = conn.recv(1024).decode() if data == "exit": conn.send(b"Good bye!\n") break conn.send(b"Hello %s!\n" % data.encode()) conn.close() print("Connection from %s:%s is closed" % addr) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)host=socket.gethostname()print(host)addr=['100.125.1.250','100.125.21.250','192.168.0.1','192.168.15.254','192.168.8.3',"114.116.48.152"]# for i in addr:# time.sleep(0.2)host=input('please host:')port=int(input('please port:'))print('stat...',s)s.bind((host, port))if s:print('ok')else:print('ng')s.listen(5)print("Waiting for connection...") while True: conn, addr = s.accept() t = threading.Thread(target = tcplink, args = (conn, addr)) t.start()
3.打包(你不用在服务器安装python)
我已经打包好啦,你可直接下载拷贝到你的[华为云](https://l.gushuji.site/huawei)服务器中(本地电脑--远程桌面)的远程桌面上下载链接:https://pan.baidu.com/s/1_9Kjm15M8wROgNnap006XA 提取码:5a92 当然你也可自己打包,我已经帮你写好了,你可直接运行(打包的操作详细参考我的博文)
打包原码: (你可直接运行以下代码进行打包)
# 1.pyinstaller打包方式1:首选 #!/usr/bin/env python3# -*- coding: utf-8 -*-import osfrom PyInstaller.__main__ import run def compile_exe_first(filename): if __name__ == '__main__': opts =[ '-D','-c', # 直接导入数据图片不会被压缩(应将资源转为.dll或二进制文件) '--add-binary=D:/PycharmProjects/test_py36/resource/picture/*.jpg;resource/picture', '--add-binary=D:/PycharmProjects/test_py36/resource/icon/*.ico;resource/icon', #导入图片.dll文件 # '--add-binary=D:/PycharmProjects/test_py36/upx/upx.exe;./', # 导入.exe文件 r'--add-data=D:\PycharmProjects\test_py36\data\tcy.txt;resource\data', #导入数据文件 r'--add-data=D:\PycharmProjects\test_py36\data\fun.htm;resource\data', #导入数据文件 # '--hidden -import = shutil.grp', #修改你要附件的路径 '--paths=D:\\PycharmProjects\\test_py36', '--paths=D:\\PycharmProjects\\test_py36\\upx', '--paths=D:\\PycharmProjects\\test_py36\\resource\\picture', '--paths=D:\\PycharmProjects\\test_py36\\resource\\icon', '--paths=C:\\ProgramData\\Anaconda3\\Library\\plugins\\PyQt5', '--paths=C:\\ProgramData\\Anaconda3\\Library\\bin', '--paths=C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\PyQt5', '--paths=C:\\ProgramData\\Anaconda3\\Lib\\site-packages', '--icon', 'D:/PycharmProjects/test_py36/resource/icon/icon.ico', '--clean', #'--noupx', '--upx-dir=./', filename #pyqt5_test.spec ] run(opts) filename='server.py' #pyqt5_test.speccompile_exe_first(filename)#================================================================================'''安装pyinstaller:最好从Anaconda Navigator平台安装 pyinstaller3.4,不要从pycharm中安装python3.6.5路径中不要有中文提醒:如果python需要用到外部的加载文件,如背景图片之类,需要按照你的程序中的要求放在相应的目录下,一般都是拷贝在生成的exe文件目录下,python程序中仅用文件名引用就行了,无需路径。'''#===============================================================================# 2.pyinstaller打包方式2:#!/usr/bin/env python3# -*- coding: utf-8 -*-#以上必须包含import osdef compile_exe(py_path,pyinstaller_path): """ :param py_path: 获取需要转换的.py格式文件位置 :param pyinstaller_path: 指定pyinstaller.exe位置 :return: None 说明:此种方式本打包开始执行文件开头必须加# -*- coding: utf-8 -*- """ file_path = py_path.rpartition('\\')[0] + '\\'# 转换后文件的放置目录 print(file_path) # 指定三个选项的文件路径 dist_path = file_path + 'dist' build_path = file_path + 'build' spec_path = file_path # 选项 options = ' -D' +r' --add-data=' \ r'D:\PycharmProjects\test_py36\resource\picture\girl.jpg;resource\picture' +\ ' --distpath ' + dist_path +\ ' --workpath ' + build_path + \ ' --specpath ' + spec_path + ' ' # 调用系统命令 os.system('python ' + pyinstaller_path + options + py_path) def test_compile_exe(): py_path=r'D:\PycharmProjects\test_py36\pyqt5_test.py' pyinstaller_path=r'C:\ProgramData\Anaconda3\Scripts\pyinstaller-script.py' compile_exe(py_path,pyinstaller_path) # test_compile_exe()
4.华为云服务器配置
登陆华为官网,配置安全组。
注意:
注意防火墙(我是全部关闭)设置完成后服务器关机后重启
5.运行(先运行服务器端程序,在运行客户端程序)
服务器端
注意你要输入你配置的ip(私网,不要用公网)和端口号
客户端运行:(ip(用公网,不要用私网)和端口号相同)
操作:你输入“字符串”,运行:显示Hello “字符串”!
6.注意:
如果不能连接大多是配置问题。云服务器配置不正确,本地配置只要能正常上网一般没什么要求。下面显示正常的配置 正常显示结果:1)[华为云](https://l.gushuji.site/huawei)服务器中: C:\Users\Administrator>netstat -n活动连接 协议 本地地址 外部地址 状态 TCP 127.0.0.1:49156 127.0.0.1:49157 ESTABLISHED TCP 127.0.0.1:49157 127.0.0.1:49156 ESTABLISHED TCP 192.168.8.3:3306 116.238.115.211:58514 ESTABLISHED#mysql服务器 TCP 192.168.8.3:3306 116.238.115.211:58515 ESTABLISHED#mysql服务器 TCP 192.168.8.3:3389 116.238.115.211:58420 ESTABLISHED#server.exe(server.py) TCP 192.168.8.3:12345 116.238.115.211:58455 ESTABLISHED TCP 192.168.8.3:49161 100.125.40.3:80 CLOSE_WAIT 2)本地电脑C:\Users\Administrator>netstat -n 活动连接 协议 本地地址 外部地址 状态 TCP 127.0.0.1:49621 127.0.0.1:49622 ESTABLISHED TCP 127.0.0.1:49622 127.0.0.1:49621 ESTABLISHED TCP 127.0.0.1:49623 127.0.0.1:49624 ESTABLISHED TCP 127.0.0.1:49624 127.0.0.1:49623 ESTABLISHED TCP 127.0.0.1:50990 127.0.0.1:50991 ESTABLISHED TCP 127.0.0.1:50991 127.0.0.1:50990 ESTABLISHED TCP 192.168.31.184:49349 221.228.75.150:9203 ESTABLISHED TCP 192.168.31.184:50949 122.246.3.93:80 CLOSE_WAIT TCP 192.168.31.184:50979 117.48.124.214:80 CLOSE_WAIT TCP 192.168.31.184:50980 114.80.177.92:443 CLOSE_WAIT TCP 192.168.31.184:50985 120.92.84.225:9002 CLOSE_WAIT TCP 192.168.31.184:50992 203.100.92.12:5222 ESTABLISHED TCP 192.168.31.184:51012 117.48.124.214:80 CLOSE_WAIT TCP 192.168.31.184:51533 117.48.124.214:80 CLOSE_WAIT TCP 192.168.31.184:51740 114.116.48.152:3389 ESTABLISHED TCP 192.168.31.184:51767 58.222.18.29:80 CLOSE_WAIT TCP 192.168.31.184:52455 117.48.124.214:80 CLOSE_WAIT TCP 192.168.31.184:52456 117.48.124.214:80 CLOSE_WAIT TCP 192.168.31.184:52457 117.48.124.214:80 CLOSE_WAIT TCP 192.168.31.184:52903 114.80.197.113:80 CLOSE_WAIT TCP 192.168.31.184:52951 101.89.125.217:80 CLOSE_WAIT TCP 192.168.31.184:52954 114.80.197.113:80 CLOSE_WAIT TCP 192.168.31.184:52955 114.80.197.113:80 CLOSE_WAIT TCP 192.168.31.184:52956 114.80.197.113:80 CLOSE_WAIT TCP 192.168.31.184:52957 114.80.197.113:80 CLOSE_WAIT TCP 192.168.31.184:52981 117.48.124.241:80 CLOSE_WAIT TCP 192.168.31.184:53018 117.48.124.214:80 CLOSE_WAIT TCP 192.168.31.184:53821 106.11.248.141:443 ESTABLISHED TCP 192.168.31.184:53912 183.36.108.16:36688 FIN_WAIT_1 TCP 192.168.31.184:53913 183.36.108.16:36688 FIN_WAIT_1 TCP 192.168.31.184:53941 183.3.225.78:36688 FIN_WAIT_1 TCP 192.168.31.184:53952 183.3.225.79:36688 TIME_WAIT TCP 192.168.31.184:53953 180.163.32.152:80 TIME_WAIT TCP 192.168.31.184:53959 183.3.225.79:36688 TIME_WAIT TCP 192.168.31.184:53964 183.3.225.78:36688 TIME_WAIT TCP 192.168.31.184:53971 183.3.225.79:36688 TIME_WAIT TCP 192.168.31.184:53981 216.58.200.238:443 SYN_SENT TCP 192.168.31.184:53982 115.239.211.112:443 FIN_WAIT_1 TCP 192.168.31.184:53983 216.58.200.238:443 SYN_SENT TCP 192.168.31.184:53984 216.58.200.238:443 SYN_SENT TCP 192.168.31.184:53985 216.58.200.238:443 SYN_SENT TCP 192.168.31.184:53986 216.58.200.238:443 SYN_SENT TCP 192.168.31.184:53987 216.58.200.238:443 SYN_SENT TCP 192.168.31.184:53988 220.181.164.91:8888 ESTABLISHED TCP 192.168.31.184:53989 115.239.211.112:443 FIN_WAIT_1 C:\Users\Administrator>
查看电脑的配置
1.华为服务器 C:\Users\Administrator>ipconfig Windows IP 配置 以太网适配器 本地连接 4: 连接特定的 DNS 后缀 . . . . . . . : openstacklocal 本地链接 IPv6 地址. . . . . . . . : fe80::c585:95b9:741a:a1a3%14 IPv4 地址 . . . . . . . . . . . . : 192.168.8.3 子网掩码 . . . . . . . . . . . . : 255.255.240.0 默认网关. . . . . . . . . . . . . : 192.168.0.1 隧道适配器 isatap.openstacklocal: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : openstacklocal 隧道适配器 Teredo Tunneling Pseudo-Interface: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : 2)本地电脑C:\Users\Administrator>ipconfig Windows IP 配置 无线局域网适配器 无线网络连接: 连接特定的 DNS 后缀 . . . . . . . : 本地链接 IPv6 地址. . . . . . . . : fe80::dda4:be60:d0b:e8f0%15 IPv4 地址 . . . . . . . . . . . . : 192.168.31.184 子网掩码 . . . . . . . . . . . . : 255.255.255.0 默认网关. . . . . . . . . . . . . : 192.168.0.1 192.168.31.1 以太网适配器 本地连接: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : 隧道适配器 isatap.{91B4435E-4B35-43A2-A2D3-3A4876D96A69}: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : 隧道适配器 Teredo Tunneling Pseudo-Interface: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : 隧道适配器 isatap.{2C673B69-0B9F-4635-92EB-A8BDC9A7F5CD}: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : C:\Users\Administrator>
有问题大家交流。