Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python请求模块和TeamCity API触发构建?_Python_Post_Build_Teamcity_Python Requests - Fatal编程技术网

如何使用Python请求模块和TeamCity API触发构建?

如何使用Python请求模块和TeamCity API触发构建?,python,post,build,teamcity,python-requests,Python,Post,Build,Teamcity,Python Requests,部分中有一个cURL示例,用于触发以下内容的生成: curl-v-u用户:密码http://teamcity.server.url:8111/app/rest/buildQueue --请求帖子——标题“内容类型:应用程序/xml”——数据二进制@build.xml 我想知道如何将其转换为等效的Python脚本(使用requests模块中的POST请求) 顺便说一句,我尝试了以下Python脚本,但得到了这样的响应代码400(错误请求): 触发构建的文档显示您需要发送XML,而不是JSON:

部分中有一个cURL示例,用于触发以下内容的生成:

curl-v-u用户:密码http://teamcity.server.url:8111/app/rest/buildQueue --请求帖子——标题“内容类型:应用程序/xml”——数据二进制@build.xml

我想知道如何将其转换为等效的Python脚本(使用
requests
模块中的
POST
请求)


顺便说一句,我尝试了以下Python脚本,但得到了这样的响应代码
400(错误请求)


触发构建的文档显示您需要发送XML,而不是JSON:


    
TeamCity RESTAPI有点复杂;有些方法同时接受XML和JSON,有些方法只接受XML。这是后一种方法之一。根据您设置的
Accept
头,它们将使用XML或JSON响应

将上述内容与您所需的构建ID一起发送;对于可以简单使用模板的XML文档:

from xml.sax.saxutils import quoteattr

template = '<build><buildType id={id}/></build>'

url = "http://myteamcity.com:8111/httpAuth/app/rest/buildQueue/"
headers = {'Content-Type': 'application/xml'}
build_id = 'MyTestBuild'
data = template.format(id=quoteattr(build_id))

r = requests.post(url, headers=headers, data=data, auth=("username", "password"), timeout=10)
从xml.sax.saxutils导入quoteattr
模板=“”
url=”http://myteamcity.com:8111/httpAuth/app/rest/buildQueue/"
headers={'Content-Type':'application/xml'}
build\u id='MyTestBuild'
data=template.format(id=quoteattr(build_id))
r=requests.post(url,headers=headers,data=data,auth=(“用户名”,“密码”),超时=10)
请注意,我使用了确保正确引用
build\u id
的值作为XML属性包含


这将产生XML;如果您想处理json响应,请将
'Accept':'application/json'
添加到
标题中。

FYI json请求在TeamCity 10中有效。

由于这个问题已经编写和回答,现在存在一个当代OSS替代方案:

要安装的Pip命令(或将
pyteamcity
添加到requirements.txt等)

pip安装pyteamcity
代码:

从pyteamcity导入TeamCity
tc=TeamCity('用户名'、'密码'、'服务器'、'端口')
结果=tc.trigger\u build('build\u id'))
打印(f'Build triggered.Web URL:{result['webUrl']})

curl示例发送的是XML,而不是JSON。为什么要发送JSON?文档有点乱;据我所知,请求模块使用json数据格式,如文档所示:这只是一个示例。只需发送XML即可。嗨,Martijn,非常感谢!如果发送XML数据,它会起作用,:)Martijn,非常感谢!在看到您的解决方案之前,我将数据指定为:
data=”“
。因为如果数据变得更复杂,我的简单方法很容易出错。
headers = {'Accept': 'application/json'}

>> r =  <Response [415]>
from xml.sax.saxutils import quoteattr

template = '<build><buildType id={id}/></build>'

url = "http://myteamcity.com:8111/httpAuth/app/rest/buildQueue/"
headers = {'Content-Type': 'application/xml'}
build_id = 'MyTestBuild'
data = template.format(id=quoteattr(build_id))

r = requests.post(url, headers=headers, data=data, auth=("username", "password"), timeout=10)