【问题标题】:Python send POST with headerPython发送带有标题的POST
【发布时间】:2012-06-01 20:51:10
【问题描述】:

我尝试构建一个 python 脚本,它发送一个带有参数的 POST 以提取结果。 使用提琴手,我提取了我想要的返回请求。该网站仅使用 https。

POST /Services/GetFromDataBaseVersionned HTTP/1.1
Host: www.mywbsite.fr
"Connection": "keep-alive",
"Content-Length": 129,
"Origin": "https://www.mywbsite.fr",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5",
"Content-Type": "application/json",
"Accept": "*/*",
"Referer": "https://www.mywbsite.fr/data/mult.aspx",
"Accept-Encoding": "gzip,deflate,sdch",
"Accept-Language": "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4",
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"Cookie": "ASP.NET_SessionId=j1r1b2a2v2w245; GSFV=FirstVisit=; GSRef=https://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CHgQFjAA&url=https://www.mywbsite.fr/&ei=FZq_T4abNcak0QWZ0vnWCg&usg=AFQjCNHq90dwj5RiEfr1Pw; HelpRotatorCookie=HelpLayerWasSeen=0; NSC_GSPOUGS!TTM=ffffffff09f4f58455e445a4a423660; GS=Site=frfr; __utma=1.219229010.1337956889.1337956889.1337958824.2; __utmb=1.1.10.1337958824; __utmc=1; __utmz=1.1337956889.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)"

{"isLeftColumn":false,"lID":-1,"userIpCountryCode":"FR","version":null,"languageCode":"fr","siteCode":"frfr","Quotation":"eu"}

现在是我的 python 脚本:

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
import string
import httplib
import urllib2

host = "www.mywbsite.fr/sport/multiplex.aspx"
    params='"isLeftColumn":"false","liveID":"-1","userIpCountryCode":"FR","version":"null","languageCode":"fr","siteCode":"frfr","Quotation":"eu"'

headers = { Host: www.mywbsite.fr,
"Connection": "keep-alive",
"Content-Length": 129,
"Origin": "https://www.mywbsite.fr",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5",
"Content-Type": "application/json",
"Accept": "*/*",
"Referer": "https://www.mywbsite.fr/data/mult.aspx",
"Accept-Encoding": "gzip,deflate,sdch",
"Accept-Language": "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4",
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"Cookie": "ASP.NET_SessionId=j1r1b2a2v2w245; GSFV=FirstVisit=;     GSRef=https://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CHgQFjAA&url=https://www.mywbsite.fr/&ei=FZq_T4abNcak0QWZ0vnWCg&usg=AFQjCNHq90dwj5RiEfr1Pw; HelpRotatorCookie=HelpLayerWasSeen=0; NSC_GSPOUGS!TTM=ffffffff09f4f58455e445a4a423660; GS=Site=frfr; __utma=1.219229010.1337956889.1337956889.1337958824.2; __utmb=1.1.10.1337958824; __utmc=1; __utmz=1.1337956889.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)"

}

url = "/Services/GetFromDataBaseVersionned"

# POST the request
conn = httplib.HTTPConnection(host,port=443)
conn.request("POST",url,params,headers)
response = conn.getresponse()

data = response.read()
print data

但是当我运行我的脚本时,我有这个错误:

socket.gaierror: [Errno -2] Name or service not known

【问题讨论】:

标签: python http post request http-headers


【解决方案1】:

要使用urllib2发出POST请求而不是GET请求,您需要指定空数据,例如:

import urllib2
req = urllib2.Request("http://am.domain.com:8080/openam/json/realms/root/authenticate?authIndexType=Module&authIndexValue=LDAP")
req.add_header('X-OpenAM-Username', 'demo')
req.add_data('')
r = urllib2.urlopen(req)

【讨论】:

    【解决方案2】:

    如果我们想将自定义 HTTP 标头添加到 POST 请求,我们必须通过字典将它们传递给headers参数。

    这是一个带有非空正文标题的示例:

    import requests
    import json
    
    url = 'https://somedomain.com'
    body = {'name': 'Maryja'}
    headers = {'content-type': 'application/json'}
    
    r = requests.post(url, data=json.dumps(body), headers=headers)
    

    Source

    【讨论】:

      【解决方案3】:

      非常感谢您提供请求模块的链接。简直太完美了。下面是我的问题的解决方案。

      import requests
      import json
      
      url = 'https://www.mywbsite.fr/Services/GetFromDataBaseVersionned'
      payload = {
          "Host": "www.mywbsite.fr",
          "Connection": "keep-alive",
          "Content-Length": 129,
          "Origin": "https://www.mywbsite.fr",
          "X-Requested-With": "XMLHttpRequest",
          "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5",
          "Content-Type": "application/json",
          "Accept": "*/*",
          "Referer": "https://www.mywbsite.fr/data/mult.aspx",
          "Accept-Encoding": "gzip,deflate,sdch",
          "Accept-Language": "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4",
          "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
          "Cookie": "ASP.NET_SessionId=j1r1b2a2v2w245; GSFV=FirstVisit=; GSRef=https://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CHgQFjAA&url=https://www.mywbsite.fr/&ei=FZq_T4abNcak0QWZ0vnWCg&usg=AFQjCNHq90dwj5RiEfr1Pw; HelpRotatorCookie=HelpLayerWasSeen=0; NSC_GSPOUGS!TTM=ffffffff09f4f58455e445a4a423660; GS=Site=frfr; __utma=1.219229010.1337956889.1337956889.1337958824.2; __utmb=1.1.10.1337958824; __utmc=1; __utmz=1.1337956889.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)"
      }
      # Adding empty header as parameters are being sent in payload
      headers = {}
      r = requests.post(url, data=json.dumps(payload), headers=headers)
      print(r.content)
      

      【讨论】:

      • 为什么将标头作为请求数据发送,为什么会这样?
      • 仅供参考,您应该手动发送这样的标题。这是请求的工作。您应该发送的最大值是用户代理和自定义标头。
      • json.dumps 是做什么的?
      • @Engin Yapici 通常我们不需要将数据转储为 json,但在某些特殊情况下我们需要它。比如在 FASTAPI 中发送 post 请求,需要将我们的数据转储为 json。
      • @Willysatrionugroho - 同意。我试图在没有json.dumps() 的情况下传递有效载荷,但失败了,非常沮丧。找到了这个答案,使用了json.dumps(),API 就像一个魅力。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 2013-01-24
      • 2016-03-13
      • 2017-03-13
      • 1970-01-01
      相关资源
      最近更新 更多