这个问题在这里已有答案:
我是客户端/服务器端编程的新手,我真的很感谢帮助解决了我几周来一直困扰的问题。
我不知道如何从网页表单向服务器发送单词,以便服务器可以将其发送到另一台计算机。
我已经尝试设置一个烧瓶框架来获取用户输入的值。然后,我尝试在同一个烧瓶应用程序中运行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
属性。
Documentation on the action attribute
@app.route('/send', methods=['POST'])
定义了服务器正在监听的端点。您需要操作来告诉它需要提交的表单。
要将表单值发送到服务器,请使用action =“http:127.0.0.1:5007”,如下所示:
<form action="http:127.0.0.1:5007" method="post" id="main" >
当然,您也可以使用任何IP地址和端口。服务器将收到以下内容:
message from the client to ther server
之后,您可以编写服务器程序来清理http请求并获取http请求体内的特定值,在我的情况下,它是单词输入的值