Python:ServiceDesk Plus Servlet API

2 投票
2 回答
2495 浏览
提问于 2025-04-16 23:47

我刚开始学习用Python编程,正在为我的公司写一个脚本。我们使用的是ServiceDesk Plus,它使用的是Servlet API。我想写一个脚本,能够在Solarwinds发出警报时自动创建或关闭工单。

我搞不清楚在Python中使用Servlet API自动创建工单的语法。以下是我写的代码(但不管用):

url = 'http://localhost:6970/servlets/RequestServlet/' 
params = urllib.urlencode({
  'operation': 'AddRequest',
})
response = urllib2.urlopen(url, params).read()

如果有人能帮帮我,我会非常感激。

编辑:

我试了James推荐的方法,但还是没成功。以下是我根据他的建议修改后的脚本。

import urllib
import urllib2

url = 'http://localhost:6970/servlets/RequestServlet/' 
params = urllib.urlencode({
    'operation': 'AddRequest',
    'username': 'lou',
    'password': 'lou',
    'requester': 'Sean Adams',
    'subject': 'Test Script Req',
    'description': 'TESTING!!!!!!',
    })
request = urllib2.Request('http://localhost:6970/servlets/RequestServlet/' ,params)
response = urllib2.urlopen(request) 

出现的错误:

C:\Users\lou\Desktop>python helpdesk2.py
Traceback (most recent call last):
  File "helpdesk2.py", line 24, in <module>
    response = urllib2.urlopen(request)
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 400, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: /servlets/RequestServlet/

2 个回答

1

也许可以先创建一个请求,然后再打开这个请求?

    params = urllib.urlencode({
  'operation': 'AddRequest',
     })
    request = urllib2.Request('http://localhost:6970/servlets/RequestServlet/' ,params)
    response = urllib2.urlopen(request)
1

你遇到了以下错误:

urllib2.HTTPError: HTTP 错误 404: /servlets/RequestServlet/

HTTP 404 错误的意思就是你请求的资源不存在。你在浏览器中打开这个页面时,使用 http://localhost:6970/servlets/RequestServlet/ 也会看到同样的错误。

造成这个错误的原因有很多,比如:

  • 这个网址真的正确吗?网址是区分大小写的哦!
  • 这个网页应用程序是否正确部署?可以查看服务器启动的日志。
  • 这个 servlet 是否正确初始化?查看网页应用程序的启动日志。
  • 这个 servlet 是否正确映射到这个网址?因为网址最后有个斜杠,所以 servlet 的映射网址模式应该是 /RequestServlet/*,假设 /servlets 是网页应用的上下文路径。如果它实际上映射到 /RequestServlet 这个网址模式,那么你应该使用没有斜杠的这个网址:http://localhost:6970/servlets/RequestServlet

撰写回答