Python:HTTP请求自动切断内容字符串

时间:2013-05-08 11:05:11

标签: python http post request

我正在尝试使用Python 2.4发送HTTP post请求。我使用wireshark分析了post请求,并且请求仅在数据中包含“options = user:jo”而不是“options = user:john”。任何建议为什么python切断最后两个字符。更短和更长的字符串也会发生这种情况。我必须使用这些库和Python 2.4。

感谢您的回复

我的剧本:

import base64
import httplib

data_string = urllib.urlencode({'options':'user:john'})

authheader =  'Basic ' + base64.encodestring("restuser:restbpm")

headers = {"Content-type": "application/x-www-form-urlencoded", "Authorization": authheader}

h = httplib.HTTPConnection('localhost:8080')

h.request('POST', '/bonita-server-rest/API/runtimeAPI/instantiateProcess/Leitungsauskunft--1.9', data_string, headers)

r = h.getresponse()

print r.read()
来自wireshark的

Еxcerpt

TCP

TCP segment data (17 bytes)  ..options=user:jo

TCP segment data (2 bytes)   hn

HTTP

Line-based text data: application/x-www-form-urlencoded

\r\n

options=user:jo

来自服务器日志

Mai 07, 2013 4:29:41 PM org.apache.catalina.valves.RequestDumperValve invoke
INFO:             header=authorization=Basic cmVzdHVzZXI6cmVzdGJwbQ==
Mai 07, 2013 4:29:41 PM org.apache.catalina.valves.RequestDumperValve invoke
INFO:             locale=de_DE
Mai 07, 2013 4:29:41 PM org.apache.catalina.valves.RequestDumperValve invoke
INFO:             method=POST
Mai 07, 2013 4:29:41 PM org.apache.catalina.valves.RequestDumperValve invoke
INFO:          parameter=
options=user:jo 
Mai 07, 2013 4:29:41 PM org.apache.catalina.valves.RequestDumperValve invoke
INFO:           pathInfo=/API/runtimeAPI/instantiateProcess/Leitungsauskunft--1.9
Mai 07, 2013 4:29:41 PM org.apache.catalina.valves.RequestDumperValve invoke
INFO:           protocol=HTTP/1.1
Mai 07, 2013 4:29:41 PM org.apache.catalina.valves.RequestDumperValve invoke
INFO:        queryString=null

Python脚本错误

<java.lang.IllegalArgumentException>
  <detailMessage>The options are null or not well set.</detailMessage>
<stackTrace>
    <trace>org.ow2.bonita.facade.impl.AbstractRemoteRuntimeAPIImpl.getAPI(AbstractRemoteRuntimeAPIImpl.java:62)</trace>
    <trace>org.ow2.bonita.facade.impl.AbstractRemoteRuntimeAPIImpl.instantiateProcess(AbstractRemoteRuntimeAPIImpl.java:178)</trace>
    <trace>sun.reflect.GeneratedMethodAccessor50.invoke(Unknown Source)</trace>
    <trace>sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)</trace>
    <trace>java.lang.reflect.Method.invoke(Method.java:601)</trace>
    <trace>org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:124)</trace>
    <trace>org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:247)</trace>
    <trace>org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:212)</trace>
    <trace>org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:202)</trace>
    <trace>org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:441)</trace>
    <trace>org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:418)</trace>
    <trace>org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:111)</trace>
    <trace>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:217)</trace>
    <trace>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:159)</trace>
    <trace>javax.servlet.http.HttpServlet.service(HttpServlet.java:717)</trace>
    <trace>org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)</trace>
    <trace>org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)</trace>
    <trace>org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)</trace>
    <trace>org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)</trace>
    <trace>org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:563)</trace>
    <trace>org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)</trace>
    <trace>org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)</trace>
    <trace>org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:615)</trace>
    <trace>org.bonitasoft.console.security.SessionFixationValve.invoke(SessionFixationValve.java:77)</trace>
    <trace>org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)</trace>
    <trace>org.apache.catalina.valves.RequestDumperValve.invoke(RequestDumperValve.java:156)</trace>
    <trace>org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)</trace>
    <trace>org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)</trace>
    <trace>org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)</trace>
    <trace>org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)</trace>
    <trace>java.lang.Thread.run(Thread.java:722)</trace>
  </stackTrace>
  <suppressedExceptions class="java.util.Collections$UnmodifiableRandomAccessList" resolves-to="java.util.Collections$UnmodifiableList">
    <c class="list"/>
    <list reference="../c"/>
  </suppressedExceptions>

将内容长度添加到标题后的服务器日志

Mai 08, 2013 2:03:19 PM org.apache.catalina.valves.RequestDumperValve invoke
INFO:          parameter=
options=user:john

使用urllib2代码的Python

import urllib2
import urllib
import base64
import sys

url = "http://localhost:8080/bonita-server-rest/API/runtimeAPI/instantiateProcess/Leitungsauskunft--1.9"
data_string = urllib.urlencode({'options':'user:john'})

request_object = urllib2.Request(url, data_string)


authheader =  'Basic ' + base64.encodestring("restuser:restbpm")

request_object.add_header('Authorization', authheader)
request_object.add_header('Content-Type','application/x-www-form-urlencoded')
request_object.add_header('Content-length','21')


try: response = urllib2.urlopen(request_object).read()
except IOError, e: 
    print e.code
    print e.headers

2 个答案:

答案 0 :(得分:2)

只是补充BrädWurst的答案。

来自base64 docs:

  

base64.encodestring(s)对包含的字符串s进行编码   任意二进制数据,并返回包含一个或多个的字符串   base64编码数据的行。 encodestring()返回一个字符串   包含一行或多行base64编码的数据,包括   一个额外的尾随换行符('\ n')。

但是,在这种情况下使用base64.standard_b64encode(s)应该会有所帮助:

import base64
authheader = 'Basic ' + base64.standard_b64encode("restuser:restbpm")
authheader
'Basic cmVzdHVzZXI6cmVzdGJwbQ=='

答案 1 :(得分:1)

我找到了问题的答案。在这一行后面

authheader =  'Basic ' + base64.encodestring("restuser:restbpm")

您必须添加此[:-1]

在这个例子中你明白为什么。

import base64
authheader =  'Basic ' + base64.encodestring("restuser:restbpm")
authheader
'Basic cmVzdHVzZXI6cmVzdGJwbQ==\n'
authheader =  'Basic ' + base64.encodestring("restuser:restbpm")[:-1]
authheader
'Basic cmVzdHVzZXI6cmVzdGJwbQ=='

通过编码,他把它放在最后\n,它在http请求中创建换行符,[:-1]切断了这个字符。

非常感谢你的帮助; - )