如何使用python从传入的httppost中提取数据

2024-10-01 05:03:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个ubuntulamp web服务器,数据通过httppost方法连续发送到webserver。我需要从httppost中提取数据并将它们插入数据库中。我不知道怎么做。关于如何处理传出的httppost请求,但是对于传入的httppost请求,有很多可用的示例。我想编写一个python3脚本,它将从传入的httppost请求中提取数据,并将它们保存为varible,我将使用它将数据插入数据库,并返回对客户。可以有人在这方面帮我吗?在


Tags: 数据方法服务器脚本web数据库示例客户
3条回答

@oetoni,我在使用时遇到超时错误:

#!/usr/bin/python3
import socketserver
from http.server import BaseHTTPRequestHandler
import time
import threading


def do_something(site_id, first, last, pass1):
    print(site_id)
    print(first)
    print(last)
    print(pass1)
    #just to illustrate the point and print the variables


class MyHandler(BaseHTTPRequestHandler):
    def do_POST(self):    # !important to use 'do_POST' with Capital POST
        global site_id, first, last, pass1  #those are still undefined at the module level ;) remember this for later
        if self.path == '/do_something':

            request_headers = self.headers

            site_id = request_headers["m_site_name"]
            first = request_headers["m_first_name"]
            last = request_headers["m_last_name"]
            pass1 = request_headers["m_device_name"]

            do_something(site_id, first, last, pass1)
        self.send_response(200)
        self.end_headers()             #as of P3.3 this is required

try:
    httpd = socketserver.TCPServer(("localhost", 9001), MyHandler)
    httpd.serve_forever()
finally:
    httpd = socketserver.TCPServer(("localhost", 9001), MyHandler)
    httpd.server_close()

但我在使用此代码时得到了正确的响应:

^{pr2}$

它在网络浏览器上打印接收到的数据。 我在apacheweb服务器上使用这个脚本作为cgi脚本,可以通过web浏览器访问它。我没有将此脚本作为服务或应用程序运行。在

#!/usr/bin/python3

# -*- coding: UTF-8 -*-

import cgi
import cgitb
cgitb.enable()

print('Content-Type: text/html\n')
arguments = cgi.FieldStorage()
print(arguments["m_site_name"].value)
print("<br />\n")
print(arguments["m_first_name"].value)
print("<br />\n")
print(arguments["m_last_name"].value)
print("<br />\n")
print(arguments["m_device_name"].value)
print("<br />\n")
site = arguments["m_site_name"].value
first = arguments["m_first_name"].value
last = arguments["m_last_name"].value
device = arguments["m_device_name"].value
  -do_other_things_with_the_variables(site,first,last,device)  -

这个代码解决了我的问题。现在我可以用这个pythoncgi脚本将httppost数据存储到变量中。在

我的HTTP POST请求: http://your_server_url_or_IP/cgi-bin/python_script.py?m_site_name=MySite&m_first_name=anyname&m_last_name=anylastanme&m_device_name=anydeviceidorname

更新

根据你在下面发布的代码,这里有一个有效的答案。在

#!/usr/bin/python3
import socketserver
from http.server import BaseHTTPRequestHandler
import time
import threading


def do_something(site_id, first, last, pass1):
    print(site_id)
    print(first)
    print(last)
    print(pass1)
    #just to illustrate the point and print the variables


class MyHandler(BaseHTTPRequestHandler):
    def do_POST(self):    # !important to use 'do_POST' with Capital POST
        global site_id, first, last, pass1  #those are still undefined at the module level ;) remember this for later
        if self.path == '/do_something':

            request_headers = self.headers

            site_id = request_headers["m_site_name"]
            first = request_headers["m_first_name"]
            last = request_headers["m_last_name"]
            pass1 = request_headers["m_device_name"]

            do_something(site_id, first, last, pass1)
        self.send_response(200)
        self.end_headers()             #as of P3.3 this is required

try:
    httpd = socketserver.TCPServer(("localhost", 9001), MyHandler)
    httpd.serve_forever()
finally:
    httpd = socketserver.TCPServer(("localhost", 9001), MyHandler)
    httpd.server_close()

跟邮递员打电话 enter image description here

命令行输出是

C:\Development\Python\test\venv\Scripts\python.exe C:/Development/Python/test/webserver_old.py
1001
jyoti0
127.0.0.1 - - [19/Nov/2018 21:53:45] "POST /do_something HTTP/1.1" 200 -
jyoti1
101

我综合了以下答案: 引用onetwo和{a4} 这也是非常重要的: https://docs.python.org/3/library/http.server.html

http.server is not recommended for production. It only implements basic security checks.

我相信对于一个小的实现和一些测试或者概念验证来说是可以的,但是最终您需要更好地管理这个问题,也许我可以建议您花一些时间使用Flask,它实际上是一个用于Python API构建和原型设计的非常好而且非常轻的框架。在

-在

上一个答案(上面已弃用并更新)

-

根据对this一个非常简单的引用:

^{pr2}$

更新(不带和API):

假设您在或计算机上运行的自定义端口的URL处有一个自定义尾随部分,那么“纯”python将如下所示:

import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler

def doSomething():
    print "i did"

class MyHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        if self.path == '/doSomething':
            mail = self.request.POST.get('email')
            something = self.request.POST.get('something')

            doSomething()
        self.send_response(200)

httpd = SocketServer.TCPServer(("", 8080), MyHandler)
httpd.serve_forever()

我假设这样你可以自由地重用变量。同时检查参考文献here,布伦达的答案。在

相关问题 更多 >