简体   繁体   English

使用python而不是cgi从html表单获取数据

[英]Get data from an html form with python without cgi

I would like get the data from a basic html form 我想从基本的html表单中获取数据

    <form method="send" action="/send">
    Name <input type="text"  name="name"/>
    <input type="submit" value="Send">
</form>

into a python file. 进入python文件。 Is that possible or not? 这有可能吗? I do not want to have to use CGI. 我不想使用CGI。 thanks Josh 谢谢约什

Whether you will need to battle with CGI, depends on your use case. 您是否需要与CGI战斗,取决于您的使用案例。

Python frameworks. Python框架。 like Flask , tend to offer their own litte web servers out-of-the-box. Flask一样,倾向于提供开箱即用的自己的小型Web服务器。 However, such servers are mostly meant for testing during the development phase. 但是,这些服务器主要用于在开发阶段进行测试。 Integration into full blown web browsers usually is via CGI or FastCGI, and can offer advanced customization and scale up to huge loads. 集成到完整的Web浏览器通常是通过CGI或FastCGI,并且可以提供高级定制并扩展到巨大的负载。

But, depending on your target audience, a good "included" web server, like the one in web2py will do for you. 但是,根据您的目标受众,一个好的“包含”的Web服务器,就像web2py中的那个,将为您做。 For learning purposes, or in a closed network behind a firewall, such solutions are perfectly ok and you aviod the tech CGI stuff. 出于学习目的,或者在防火墙后面的封闭网络中,这样的解决方案完全没问题,您可以避免使用技术CGI。 Once your app is finished or the "included" web server will not suit your needs anymore, you can still deploy your app behind a "real" web server. 一旦您的应用程序完成或“包含”的Web服务器不再适合您的需求,您仍然可以将您的应用程序部署在“真正的”Web服务器之后。

If you really like it the tough way, you may also consider to write your own web server from scratch ;) 如果您真的很喜欢它,那么您也可以考虑从头开始编写自己的Web服务器 ;)

Python work on server side. Python在服务器端工作。 Browser work on client side. 浏览器在客户端工作。 They communicate via network. 他们通过网络进行沟通 So it is imposible to do that without CGI ar any CGI-like method (FastCGI, PSGI, WSGU etc...). 因此,没有CGI和任何类似CGI的方法(FastCGI,PSGI,WSGU等......)这样做是不可能的。 The best way for Python is WSGI. Python的最佳方式是WSGI。 Getting started wsgi http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html Process form example http://eddmann.com/posts/understanding-python-wsgi-with-examples/ 入门wsgi http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html流程表示例http://eddmann.com/posts/understanding-python-wsgi-with-examples/

If I am understanding your question correctly you could use the selenium module. 如果我正确理解您的问题,您可以使用selenium模块。 As an example, if you wanted to get the source code from Google, your code would be: 例如,如果您想从Google获取源代码,您的代码将是:

from selenium import webdriver
driver = webdriver.Chrome() # Or Firefox, or whatever browser you use
driver.get('https://google.com')
data = driver.page_source

The source code for Google is now stored in the data variable. Google的源代码现在存储在数据变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM