sometimes,httplib2 connot get response through goproxy
When I use httplib2 to call api in python, i can't get the response, but in fact, goproxy has received a response. For example: In [16]: from httplib2 import Http In [17]: h=Http(timeout=60) In [18]: a,b=h.request(u,'POST',d,headers={'content-type':'application/json'}) --------------------------------------------------------------------------- timeout
The goproxy log is as follow: Type: request ReceivedAt: 2015-09-06 19:38:32.561017311 +0800 CST Session: 3 From:
POST /jsonrpc/inventory2?api_key=no HTTP/1.1 Host: 10.12.22.206 Accept-Encoding: gzip, deflate Content-Type: application/json User-Agent: Python-httplib2/0.9.1 (gzip)
Type: response ReceivedAt: 2015-09-06 19:38:32.582834713 +0800 CST Session: 3 From:
HTTP/1.1 200 OK Connection: close Content-Length: 0 Accept-Encoding: gzip, deflate Access-Control-Allow-Origin: * Api_key: no Breadcrumbid: e9aa6927-8f02-4b80-9f2f-32b229b03dde Content-Type: application/json Server: Jetty(8.1.17.v20150415) User-Agent: Python-httplib2/0.9.1 (gzip) X-Forwarded-For: 10.6.208.10
When I use requests package to call the same api, i can get the response.For example: In [19]: import requests In [20]: c=requests.post(u,d,headers={"content-type":"application/json"}) In [21]: c.text Out[21]: u'{"jsonrpc":"2.0","id":179337267,"error":{"code":-32601,"data":null,"message":"Method not found: queryApproveHistory"}}'
How do I solve this problem?
My code is: func main() { verbose := flag.Bool("v", false, "should every proxy request be logged to stdout") addr := flag.String("h", ":9090", "on which address should the proxy listen") flag.Parse() proxy := goproxy.NewProxyHttpServer() proxy.Verbose = *verbose
proxy.Tr.Dial = func(network, addr string) (net.Conn,error){
return net.DialTimeout(network, addr, 10 * time.Second)
}
proxy.Tr.ResponseHeaderTimeout = time.Second * 1200
uk_prix := time.Now().Format("060102150405")
log_dir_name := "goproxylog/" + uk_prix
if err := os.MkdirAll(log_dir_name, 0755); err != nil {
log.Fatal("Can't create dir", err)
}
logger, err := NewLogger(log_dir_name)
if err != nil {
log.Fatal("can't open log file", err)
}
proxy.OnRequest().HandleConnectFunc(func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
return goproxy.HTTPMitmConnect,host
})
tr := transport.Transport{Proxy: transport.ProxyFromEnvironment}
proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
ctx.RoundTripper = goproxy.RoundTripperFunc(func (req *http.Request, ctx *goproxy.ProxyCtx) (resp *http.Response, err error) {
ctx.UserData, resp, err = tr.DetailedRoundTrip(req)
return
})
logger.LogReq(req, ctx, uk_prix)
return req, nil
})
proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
logger.LogResp(resp, ctx, uk_prix)
return resp
})
l, err := net.Listen("tcp", *addr)
if err != nil {
log.Fatal("listen:", err)
}
sl := newStoppableListener(l)
ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt)
go func() {
<-ch
log.Println("Got SIGINT exiting")
sl.Add(1)
sl.Close()
logger.Close()
sl.Done()
}()
log.Println("Starting Proxy")
http.Serve(sl, proxy)
sl.Wait()
log.Println("All connections closed - exit")
}