社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

7stud

7stud 最近创建的主题
7stud 最近回复了

这是我的设置:

elasticsearch Version: 7.0.1
{:httpoison, "~> 1.5"}  #=> mix.lock shows version 1.5.1 was installed

:

$ curl -XGET "http://localhost:9200"
{
  "name" : "My-MacBook-Pro.local",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "vEFl3B5TTYaBxPhQFuXPyQ",
  "version" : {
    "number" : "7.0.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "e4efcb5",
    "build_date" : "2019-04-29T12:56:03.145736Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.7.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

HTTPoison结果:

$ iex -S mix
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

===> Compiling parse_trans
===> Compiling mimerl
===> Compiling metrics
===> Compiling unicode_util_compat
===> Compiling idna
==> ssl_verify_fun
Compiling 7 files (.erl)
Generated ssl_verify_fun app
===> Compiling certifi
===> Compiling hackney
==> httpoison
Compiling 3 files (.ex)
Generated httpoison app
==> hello
Compiling 15 files (.ex)
Generated hello app
Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> HTTPoison.get "http://localhost:9200"
{:ok,
 %HTTPoison.Response{
   body: "{\n  \"name\" : \"My-MacBook-Pro.local\",\n  \"cluster_name\" :  
\"elasticsearch\",\n  \"cluster_uuid\" : \"vEFl3B5TTYaBxPhQFuXPyQ\",\n  
\"version\" : {\n    \"number\" : \"7.0.1\",\n    \"build_flavor\" :   
\"default\",\n    \"build_type\" : \"tar\",\n    \"build_hash\" :   
\"e4efcb5\",\n    \"build_date\" : \"2019-04-29T12:56:03.145736Z\",\n  
\"build_snapshot\" : false,\n    \"lucene_version\" : \"8.0.0\",\n  
\"minimum_wire_compatibility_version\" : \"6.7.0\",\n  
\"minimum_index_compatibility_version\" : \"6.0.0-beta1\"\n  },\n  
\"tagline\" : \"You Know, for Search\"\n}\n",
   headers: [
     {"content-type", "application/json; charset=UTF-8"},
     {"content-length", "522"}
   ],
   request: %HTTPoison.Request{ 
     body: "",
     headers: [],
     method: :get,
     options: [],
     params: %{},
     url: "http://localhost:9200"
   },
   request_url: "http://localhost:9200",
   status_code: 200
 }}

iex(2)> 

接下来,我停止了elasticsearch服务器,然后再次运行HTTPoison请求:

ex(2)> HTTPoison.get "http://localhost:9200"
{:error, %HTTPoison.Error{id: nil, reason: :econnrefused}}

对于curl请求,我得到了类似的结果:

$ curl -XGET "http://localhost:9200"
curl: (7) Failed to connect to localhost port 9200: Connection refused

7 年前
回复了 7stud 创建的主题 » 服务器端使用python的post请求句柄[duplicate]

您应该像这样读取json数据:

#!/usr/bin/env python3

import os
import sys
import json

content_len = int(os.environ["CONTENT_LENGTH"])

req_body = sys.stdin.read(content_len)
my_dict = json.loads(req_body)

使用以下代码,可能会遇到问题:

 myjson = json.load(sys.stdin)

或者写得不那么简洁:

requ_body = sys.stdin.read()
my_dict = json.load(requ_body)

那个 当我的cgi脚本在 apache 服务器,但你不能指望它能正常工作——正如我发现我的CGI脚本在另一台服务器上时所做的那样。根据CGI规范:

RFC 3875                    CGI Version 1.1                 October 2004


4.2.  Request Message-Body

   Request data is accessed by the script in a system-defined method;
   unless defined otherwise, this will be by reading the 'standard
   input' file descriptor or file handle.

      Request-Data   = [ request-body ] [ extension-data ]
      request-body   = <CONTENT_LENGTH>OCTET
      extension-data = *OCTET

   A request-body is supplied with the request if the CONTENT_LENGTH is
   not NULL.  The server MUST make at least that many bytes available
   for the script to read.  The server MAY signal an end-of-file
   condition after CONTENT_LENGTH bytes have been read or it MAY supply
   extension data.  Therefore, the script MUST NOT attempt to read more
   than CONTENT_LENGTH bytes, even if more data is available.  However,
   it is not obliged to read any of the data.

关键是:

脚本不能试图读取更多 比内容长度字节长,即使有更多数据可用。

显然地, 阿帕奇 在向cgi脚本发送请求正文后立即向cgi脚本发送eof信号,这会导致 sys.stdin.read() 回来。但是根据cgi规范,服务器不需要在请求主体之后发送eof信号,我发现我的cgi脚本挂起了 系统stdin.read() --当我的脚本在另一台服务器上时,最终导致超时错误。

因此,为了在一般情况下读入json数据,您应该执行以下操作:

content_len = int(os.environ["CONTENT_LENGTH"])

req_body = sys.stdin.read(content_len)
my_dict = json.loads(req_body)

服务器为cgi脚本设置一组环境变量,这些变量包含头信息,其中之一是内容长度。

下面是当我使用 myjson = json.load(sys.stdin) :

-v      verbose output
-H      specify one header
--data  implicitly specifies a POST request 

Note that curl automatically calculates a Content-Length header 
for you.

~$ curl -v \
> -H 'Content-Type: application/json' \
> --data '{"a": 1, "b": 2}' \
> http://localhost:65451/cgi-bin/1.py

*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 65451 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 65451 (#0)
> POST /cgi-bin/1.py HTTP/1.1
> Host: localhost:65451
> User-Agent: curl/7.58.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 16
> 
* upload completely sent off: 16 out of 16 bytes

=== hung here for about 5 seconds ====

< HTTP/1.1 504 Gateway Time-out
< Date: Thu, 08 Mar 2018 17:53:30 GMT
< Content-Type: text/html
< Server: inets/6.4.5
* no chunk, no close, no size. Assume close to signal end
< 
* Closing connection 0