我有一个Ubuntu LAMP Web服务器,并且数据正在通过HTTP POST方法连续发送到该Web服务器。我需要从HTTP POST中提取数据并将其插入数据库中。我不知道该怎么做。关于如何处理传出的HTTP POST请求但传入的HTTP POST请求有很多示例。我想编写一个python3脚本,该脚本将从传入的HTTP POST请求中提取数据并将其保存为变量,以便将数据插入数据库并返回响应给客户端。在这方面有人可以帮助我吗?
答案 0 :(得分:0)
更新
根据您在下面发布的代码,这是一个有效的答案。
#!/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()
,命令行输出为
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
我在这里结合了这些答案: 参考one,two和third 这对于阅读也很重要: https://docs.python.org/3/library/http.server.html
不建议将http.server用于生产。它仅实现基本的安全检查。
我认为可以进行一个较小的实施,并进行一些测试或概念验证,但最终您需要更好地进行管理,也许我可以建议您花一些时间并使用Flask,这实际上是优秀且非常轻便的Python API构建和原型开发框架。
-
根据对this一个非常简单的引用:
def do_POST(self):
# Doesn't do anything with posted data
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
self._set_headers()
self.wfile.write("<html><body><h1>POST!</h1></body></html>")
更新(不包含API)
假设您在自定义端口上运行或在机器上运行,URL上带有自定义尾部,那么“ pure” 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,这是Brenda的答案。
答案 1 :(得分:0)
@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()
但是在使用此代码时,我得到了正确的响应:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import cgi
import cgitb
cgitb.enable()
print('Content-Type: text/html')
print('')
arguments = cgi.FieldStorage()
for i in arguments.keys():
print(arguments[i].value)
,它将在Web浏览器上打印接收到的数据。 我将此脚本用作可通过Web浏览器访问的apache Web服务器上的cgi脚本。我没有将此脚本作为服务或应用程序运行。
答案 2 :(得分:0)
#!/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)-----
此代码解决了我的问题。现在,我可以使用此python cgi脚本将HTTP POST数据存储到变量中。