2013-11-03 58 views
1

我有下面的python腳本,它正在發出成功的其他Web服務請求,但在服務器發送HTTP 200後發出404請求。我可以看到其他Web服務Web應用程序日誌成功返回下面的JSON響應。任何想法如何進一步排除故障或可能導致下面的錯誤?即使發送了HTTP 200,urllib2.urlopen(req)也會返回404

執行

最後的Python行:

result = urllib2.urlopen(req) 

從REST API端點JSON響應:

response{"status":"SUCCESS","reason":null,"location":null,"details":null,"errorDetails":{}} 

- Python腳本:

#!/home/user/activepython-2.7.2.5_x86_64/bin/python 

import sys 
import os 
import logging 
import subprocess 
import tempfile 
import json 
import urllib2 

# define SVN 
svn_repo = sys.argv[1] 
svn_txn = sys.argv[2] 
svn_opt = '-t' 

# handle temp file 
tmp_file = tempfile.NamedTemporaryFile(prefix="app_", 
             suffix=".tmp", dir="/tmp", delete=False) 
delete_file = True 

rest_url = 'https://host/rest/api/endpoint' 

# setup logging 
log_level = logging.DEBUG 
logger = logging.getLogger("myapp") 
logger.setLevel(log_level) 
handler = logging.StreamHandler(sys.stderr) 
handler.setLevel(log_level) 
logger.addHandler(handler) 

def get_svn_changes(): 
    cmd = "/home/user/bin/svnlook changed --copy-info %s %s %s" % (svn_opt, svn_txn, svn_repo) 
    output, return_code = command_output(cmd) 
    return output 

def get_author(): 
    cmd = "/home/csvn/bin/svnlook author %s %s %s" % (svn_opt, svn_txn, svn_repo) 
    author, return_code = command_output(cmd) 
    return author.strip() 

def call_webservice(): 
    req = urllib2.Request(rest_url) 
    req.add_header('Accept', 'arpplication/json') 
    req.add_header('Content-Type', 'application/json') 
    logger.debug("file=" + tmp_file.name) 
    data = json.dumps({"name" : "file", "value" : tmp_file.name}) 
    logger.debug("data=" + data) 
    req.add_data(data) 

    logger.debug("request") 
    result = urllib2.urlopen(req) 
    logger.debug("result") 
    json_result = json.load(result) 
    logger.debug("json_result") 
    result_data = json.loads(json_result['response']) 
    logger.debug("result_data") 
    return result_data 

if __name__ == "__main__": 

    exit_code = 0; 
    out_message = '' 
    author = get_author() 

    try: 
     tmp_file.write("author=%s\n" % author) 
     output = get_svn_changes() 
     tmp_file.write(output) 
     tmp_file.close() 
     output = call_webservice() 

     if (output['status'] == 'ERROR'): 
      out_message = output['reason'] 
      exit_code = 1 

    except Exception, ex: 
     out_message = str(ex) 
     exit_code = 1 

    finally: 
     if (exit_code == 1): 
      sys.stderr.write("Error: %s" % out_message) 
     if delete_file: 
      os.remove(tmp_file.name) 
    sys.exit(exit_code) 

回溯例外:

//startlogger output 
file=/tmp/app_rrOgN0.tmp 
data={"name": "file", "value": "/tmp/app_rrOgN0.tmp"} 
request 
//stop logger output 
Traceback (most recent call last): 
    File "/home/csvn/data/repositories/repo/hooks/pre-commit", line 85, in <module> 
    output = call_webservice() 
    File "/home/csvn/data/repositories/repo/hooks/pre-commit", line 59, in call_webservice 
    result = urllib2.urlopen(req) 
    File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 126, in urlopen 
    return _opener.open(url, data, timeout) 
    File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 400, in open 
    response = meth(req, response) 
    File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 513, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 438, in error 
    return self._call_chain(*args) 
    File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 372, in _call_chain 
    result = func(*args) 
    File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 521, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
HTTPError: HTTP Error 404: Not Found 
Error: HTTP Error 404: Not Found 
+0

您是不是要找設置'Accept'頭' arpplication/json'?請注意那裏的「應用程序」的拼寫錯誤。 –

+0

異常來自另一端的服務器;它是*服務器*返回一個404,而不是'urllib2'隨機產生一個不同的狀態碼。 –

+0

它是應用程序/ json,請添加作爲答案,所以我可以給你信用 – c12

回答

3

拼錯Accept頭:

req.add_header('Accept', 'arpplication/json') 

糾正application拼寫:

req.add_header('Accept', 'application/json') 
相關問題