我的问题
我在谷歌colab中使用kivy和buildozer创建了一个移动聊天应用程序。它在我的电脑上运行得很好,但它无法使用本地网络中的手机连接到我的服务器。
**我的 .spec 文件的一部分 **
注意:我在 buildozer.spec 文件中授予了互联网权限,如下所示: android.permissions = android.permission.INTERNET, (name=android.permission.WRITE_EXTERNAL_STORAGE;maxSdkVersion=18)
这是我的要求部分 要求= python3,kivy,rsa
我的代码
我的代码如下。首先我获取服务器 IP 和昵称。建立连接后,我尝试隐藏与 ip 和 nikname 部分相对应的部分。最后,用户尝试将他的消息发送给其他人。 这个应用程序在我的电脑上运行得很好,但它无法连接到我的服务器。
import kivy
import socket
import threading
import rsa
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import mainthread
kivy.require('1.9.0')
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
public_key, privet_key= rsa.newkeys(1024)
class MyRoot(BoxLayout):
server_public_key=None
def __init__(self):
super(MyRoot, self).__init__()
@mainthread
def update_chat_text(self, message):
self.chat_text.text += message + "\n"
def send_message(self):
client.send(rsa.encrypt(f"{self.nickname_text.text}: {self.message_text.text}".encode(), self.server_public_key))
def connect_to_server(self):
if self.nickname_text.text != "":
client.connect((self.ip_text.text, 9999))
message = client.recv(1024).decode('utf-8')
if message == "NICK":
client.send(self.nickname_text.text.encode('utf-8'))
self.server_public_key=rsa.PublicKey.load_pkcs1(client.recv(1024))
client.send(public_key.save_pkcs1('PEM'))
self.send_btn.disabled = False
self.message_text.disabled = False
self.connect_btn.disabled = True
self.ip_text.disabled = True
self.make_invisible(self.connection_grid)
self.make_invisible(self.connect_btn)
thread = threading.Thread(target=self.receive)
thread.start()
def make_invisible(self, widget):
widget.visible = False
widget.size_hint_x = None
widget.size_hint_y = None
widget.height = 0
widget.width = 0
widget.text = ""
widget.opacity = 0
def receive(self):
stop = False
while not stop:
try:
message = rsa.decrypt(client.recv(1024), privet_key).decode()
self.update_chat_text(message)
except Exception as ex:
print("ERROR:", ex)
client.close()
stop = True
class NeuralWebChat(App):
def build(self):
return MyRoot()
neuralWebChat = NeuralWebChat()
neuralWebChat.run()
这是我的neuralwebchat.kv
在本节中我提供了我的 .kv 文件
<MyRoot>
orientation: 'vertical'
ip_text: ip_text
nickname_text: nickname_text
connection_grid: connection_grid
connect_btn: connect_btn
chat_text: chat_text
send_btn: send_btn
message_text: message_text
GridLayout:
id: connection_grid
rows: 1
cols: 2
padding: 10
spacing: 10
height: 125
size_hint: (1,None)
background_color: 0.3, 0.3, 0.3, 0.2
BoxLayout:
orientation: 'vertical'
Label:
text: "Server IP"
font_size: 42
color: 0.2, 0.6, 0.8, 1
#rgb opacity
TextInput:
id: ip_text
size_hint: (1, None)
height: 50
font_size: 36
BoxLayout:
orientation: 'vertical'
Label:
text: "Nickname_text"
font_size: 42
color: 0.2, 0.6, 0.8, 1
TextInput:
id: nickname_text
size_hint: (1, None)
height: 50
font_size: 36
Button:
id: connect_btn
text: "Connect"
font_size: 36
size: 100, 50
size_hint: (1, None)
height: 70
background_color: 0.2, 0.6, 0.8, 1
on_press: root.connect_to_server()
Label:
text: "Chat History"
font_size: 42
height: 50
size_hint: (1, None)
color: 0.2, 0.6, 0.8, 1
TextInput:
id: chat_text
size_hint_y: None
height: 350
size_hint: (1, None)
font_size: 36
readonly: True
disabled: True
multiline: True
background_color: 0.9, 0.9, 0.9, 1
Label:
text: "Your Message"
font_size: 42
color: 0.2, 0.6, 0.8, 1
size_hint: (1, None)
height: 50
TextInput:
id: message_text
font_size: 36
disabled: True
size_hint: (1, None)
background_color: 1, 1, 1, 1
Button:
id: send_btn
text: "Send"
font_size: 36
size: 100, 50
background_color: 0.2, 0.6, 0.8, 1
on_press: root.send_message()
disabled: True
我是编码新手,将不胜感激任何帮助。