我正在尝试制作一个消息应用程序,但我不知道如何将python客户端连接到html服务器。这是我当前的客户端代码:
from tkinter import *
is_logged_in = False
username = 'Error'
class Startup:
def __init__(self):
self.root = Tk()
self.root.title("Disbroad V0.01: Main Menu")
self.bg_gray = "#ABB2B9"
self.bg_color = "#17202A"
self.light_blue = '#B6C8FF'
self.text_color = "#EAECEE"
self.font = "Helvetica 14"
self.bold_font = "Helvetica 13 bold"
if is_logged_in is False:
Label(self.root, bg=self.light_blue, text="Welcome to Disbroad", font=self.bold_font, pady=10, width=35,
height=1).grid(row=0)
Button(self.root, text="Login", font=self.bold_font, bg=self.bg_gray, command=self.login).grid(row=1,
column=0)
Button(self.root, text="Create Account", font=self.bold_font, bg=self.bg_gray,
command=self.create_account).grid(row=3)
Button(self.root, text="Quit App", font=self.bold_font, bg=self.bg_gray, command=quit).grid(row=4)
elif is_logged_in is True:
print('Client ready')
self.root.mainloop()
def create_account(self):
self.root.destroy()
self.create_root = Tk()
self.create_root.title("Disbroad V0.01: Create")
self.username = StringVar()
self.password = StringVar()
self.confirm_password = StringVar()
self.pass_match = StringVar()
Label(self.create_root, bg=self.light_blue, text="Create Account", font=self.bold_font, pady=10, width=30,
height=1).grid(row=0)
Label(self.create_root, text='Username:').grid(row=1, column=0)
Entry(self.create_root, textvariable=self.username).grid(row=2, column=0)
Label(self.create_root, text='Password:').grid(row=3, column=0)
Entry(self.create_root, textvariable=self.password).grid(row=4, column=0)
Label(self.create_root, text='Confirm Password:').grid(row=5, column=0)
Entry(self.create_root, textvariable=self.confirm_password).grid(row=6, column=0)
Label(self.create_root, textvariable=self.pass_match).grid(row=7, column=0)
Button(self.create_root, text='Create Account', command=self.check_passwords).grid(row=8, column=0)
Button(self.create_root, text='Return to Menu', command=self.create_to_menu).grid(row=9, column=0)
def check_passwords(self):
self.pass_match.set('')
if self.password.get() == self.confirm_password.get() and self.password.get() != ''
and self.confirm_password.get() != '':
print('Passwords Match')
self.pass_match.set('Passwords match')
elif self.password.get() != self.confirm_password.get():
print('Passwords do not match')
self.pass_match.set('Passwords do not match')
elif self.password.get() == '' and self.confirm_password.get() == '':
self.pass_match.set('Invalid passwords')
def create_to_menu(self):
self.create_root.destroy()
self.__init__()
def login(self):
self.root.destroy()
self.login_root = Tk()
self.login_root.title("Disbroad V0.01: Login")
self.username = StringVar()
self.password = StringVar()
Label(self.login_root, bg=self.light_blue, text="Login to Account", font=self.bold_font, pady=10, width=30,
height=1).grid(row=0)
Label(self.login_root, text='Username:').grid(row=1, column=0)
Entry(self.login_root, textvariable=self.username).grid(row=2, column=0)
Label(self.login_root, text='Password:').grid(row=3, column=0)
Entry(self.login_root, textvariable=self.password).grid(row=4, column=0)
Button(self.login_root, text='Login to Account', command=self.username.get).grid(row=5, column=0)
Button(self.login_root, text='Return to Menu', command=self.login_to_menu).grid(row=6, column=0)
def login_to_menu(self):
self.login_root.destroy()
self.__init__()
这是我当前的服务器代码:
import logging
from datetime import datetime
import sys
import socket
class Startup:
def __init__(self):
now = datetime.now()
dt = now.strftime("%d.%m.%Y")
# Initialising logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(message)s',
handlers=[
logging.FileHandler(f"Logs/Server_Logs {dt}.txt"),
logging.StreamHandler(sys.stdout)
]
)
server_host = '172.20.10.7'
server_port = 8000
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((server_host, server_port))
server_socket.listen(1)
print('Listening on port %s ...' % server_port)
while True:
# Wait for client connections
client_connection, client_address = server_socket.accept()
# Get the client request
request = client_connection.recv(1024).decode()
print(request)
# Send HTTP response
response = 'HTTP/1.0 200 OKnnBig Peens'
client_connection.sendall(response.encode())
client_connection.close()
# Close socket
# server_socket.close()
logging.info(' Server is ready')
print('Server is Ready')
我想知道是否有可能将服务器和客户端连接在一起,如果有,那将是一个很大的帮助,并使这个应用程序更容易制作。
您很可能想要使用Requests-python库。它使您可以非常容易地发出HTTP请求。
在您的情况下,客户端中的示例可能类似于以下片段:
r = requests.get('http://172.20.10.7:8000', timeout=1000)
print(r.text)