python - 如何将字符串从用户表单发送到服务器?
问题描述
我是客户端/服务器端编程的新手,我真的会为我已经坚持了几个星期的问题提供帮助。
我不知道如何将网页表单中的单词发送到服务器,以便服务器可以将其发送到另一台计算机。
我尝试建立一个烧瓶框架来获取用户输入的值。然后,我尝试在同一个烧瓶应用程序中运行 tcp 客户端代码,以将其发送到我的 tcp 服务器。它不起作用,我想知道这是否是烧瓶的正确用法,因为我也没有使用烧瓶的经验。
这是我的烧瓶应用程序代码:
#client_app.py
#from the Flask class insert the flask library
from flask import Flask, render_template, request
import socket
import sys
#this is a route
@app.route('/send', methods=['POST'])
def send():
puzzle_word = request.form['word']
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print('connecting to {} port {}'.format(*server_address))
sock.connect(server_address)
try:
# Send data
message = b'Salam Alaikum.'
print('sending {!r}'.format(message))
sock.sendall(message)
# Look for the response
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = sock.recv(1024)
amount_received += len(data)
print('received {!r}'.format(data))
finally:
print('closing socket')
sock.close()
这是我的 tcp 服务器
#socket_echo_server.py
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
print('waiting for a connection')
connection, client_address = sock.accept()
try:
print('connection from', client_address)
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(1024)
print('received {!r}'.format(data))
if data:
print('sending data back to the client')
connection.sendall(data)
else:
print('no data from', client_address)
break
finally:
# Clean up the connection
connection.close()
这是我的网络表单的核心部分
<form id="main" method="POST" onsubmit="app_client.py" >
<h2 class="sr-only">Login Form</h2>
<div class="two-colored-border">Game Rules: <ol> <li> A word must be 2-4 letters </li> <li> A word contains only Alphabets </li> <li> Letters can be lower case, upper case, or a combination of both </li> </ol> </div>
<div id="myForm" class="form-group">
<label for="word">Please select a word:</label>
<input name="word" id="userInput" class="form-control" type="text" required minlength="2" maxlength="5" onkeyup="lettersOnly(this)" >
<button id="sendbutton" class="btn btn-primary btn-block" type="submit">Submit</button>
</div>
</form>
本质上,我不知道如何将字符串从网页发送到另一台计算机。
很抱歉这个问题很长,我试图提供尽可能多的适用细节,我希望有任何帮助!
解决方案
<form id="main" method="POST" onsubmit="app_client.py" >
您试图将表单指向您的代码,但onsubmit
用于在浏览器中运行脚本。您想要的是将 POST 消息发送到您的服务器的表单,并且您使用该action
属性。
@app.route('/send', methods=['POST'])
定义服务器正在侦听的端点。您需要该操作来告诉它需要在那里提交的表单。
推荐阅读
- android - 如何在 ImageView 下将 TextView 居中?
- excel - 从函数返回服务器错误作为字符串?
- pandas - 在执行 K-Means 算法时检索索引
- python - 为什么以及何时使用新的 anaconda 环境?(一个环境适合一切?每个项目都有一个新环境?)
- python-3.x - 在 9 秒内打印尽可能多的数字,然后在第 10 秒打印“-1”
- html - 为由两个相邻 div 和剪辑路径生成的形状添加边框
- google-chrome - 无法修改 chrome 首选项文件中“prompt_for_download”的值
- flutter - Dart Flutter 如何在其类内部初始化一个类方法?
- flutter - Flutter + Get, toNamed() 返回 RouteSettings() 错误
- python - 在 python 中运行多个 sql 语句的建议方法?