我尝试创建Python服务器(使用Python 3)从客户端(使用Kivy开发)接收信息。我试图使用套接字,但我得到错误: "文字输入'不支持缓冲接口" 如果你告诉我我的错误,我会很感激:)
我的客户:
from kivy.app import App
#kivy.require("1.8.0")
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
import socket
class LoginScreen(GridLayout):
def __init__(self,**kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols=2
my_socket = socket.socket()
host=socket.gethostname()
my_socket.connect((host,80))
self.add_widget(Label(text='username'))
self.username = TextInput(multiline=False)
self.add_widget(self.username)
my_socket.send(self.username)
my_socket.close()
self.add_widget(Label(text='Password'))
self.password = TextInput(multiline=False,password=True)
self.add_widget(self.password)
class SimpleKivy(App):
def build(self):
return LoginScreen()
if __name__=='__main__':
SimpleKivy().run()
我的服务器:
import socket
import db # import my db
server_socket = socket.socket()
server_socket.bind(('0.0.0.0',80))
server_socket.listen(5)
(client_socket, client_address) = server_socket.accept()
client_username = client_socket.recv(1024)
data_entry_login(client_username,"dsfsds","dsfsfsdd","2") #insert to my db
client_socket.close()
server_socket.close()
感谢您的帮助:)
答案 0 :(得分:0)
关注缓冲区的问题,需要将bytes()
发送到套接字。它没有任何字符串。
我认为使用套接字是如此低级别,而不是这里的最佳做法。尝试开发一个接受客户端请求的rest API,并为您执行db操作。 CherryPy是如此微不足道,有利于为此目的轻松开始。回到客户端代码,尝试创建Button
并将其绑定到将用户名发送到服务器的方法。在当前代码中,您将kivy.uix.textinput.TextInput
对象发送到服务器,而您需要使用对象的text
属性发送它的文本。我建议进行以下修改
from kivy.app import App
# kivy.require("1.8.0")
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
import socket
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 3
self.my_socket = socket.socket()
host = socket.gethostname()
port = 8585
self.my_socket.connect((host, port))
self.add_widget(Label(text='username'))
self.username = TextInput(multiline=False)
self.add_widget(self.username)
self.add_widget(Label(text='Password'))
self.password = TextInput(multiline=False, password=True)
self.add_widget(self.password)
self.submit_button = Button(text='Submit')
self.submit_button.bind(on_press=self.submit_username)
self.add_widget(self.submit_button)
def submit_username(self, *args):
# Make sure to validate the input before submitting to the server
self.my_socket.send(bytes(self.username.text))
class SimpleKivy(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
SimpleKivy().run()
答案 1 :(得分:0)
我的客户代码:
from kivy.app import App
# kivy.require("1.8.0")
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
import socket
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 3
self.my_socket = socket.socket()
#host = socket.gethostname()
#port = 8585
self.my_socket.connect(('127.0.0.1', 8080))
self.add_widget(Label(text='username'))
self.username = TextInput(multiline=False)
self.add_widget(self.username)
self.add_widget(Label(text='Password'))
self.password = TextInput(multiline=False, password=True)
self.add_widget(self.password)
self.submit_button = Button(text='Submit')
self.submit_button.bind(on_press=self.submit_username)
self.add_widget(self.submit_button)
def submit_username(self, *args):
# Make sure to validate the input before submitting to the server
self.my_socket.send(self.username.text.encode('utf-8'))
class SimpleKivy(App):
def build(self):
return LoginScreen()
这是我的服务器代码:
import socket
import db # import my db
server_socket = socket.socket()
server_socket.bind(('127.0.0.1',8080))
server_socket.listen(5)
(client_socket, client_address) = server_socket.accept()
client_username = client_socket.recv(1024)
data_entry_login(client_username,"dsfsds","dsfsfsdd","2") #insert to my db
client_socket.close()
server_socket.close()
也许我需要在客户端代码中编写:self.my_socket.close()?