首页 > 解决方案 > 如何将字符串从用户表单发送到服务器?

问题描述

我是客户端/服务器端编程的新手,我真的会为我已经坚持了几个星期的问题提供帮助。

我不知道如何将网页表单中的单词发送到服务器,以便服务器可以将其发送到另一台计算机。

我尝试建立一个烧瓶框架来获取用户输入的值。然后,我尝试在同一个烧瓶应用程序中运行 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>

本质上,我不知道如何将字符串从网页发送到另一台计算机。

很抱歉这个问题很长,我试图提供尽可能多的适用细节,我希望有任何帮助!

标签: pythonflasknetwork-programminghttp-post

解决方案


<form id="main" method="POST" onsubmit="app_client.py" >

您试图将表单指向您的代码,但onsubmit用于在浏览器中运行脚本。您想要的是将 POST 消息发送到您的服务器的表单,并且您使用该action属性。

关于 action 属性的文档

@app.route('/send', methods=['POST'])定义服务器正在侦听的端点。您需要该操作来告诉它需要在那里提交的表单。


推荐阅读