在以下代码中,就像在此站点上看到的那样
https://cloud.google.com/compute/docs/instances/create-start-instance
def addInstance(http, listOfHeaders):
url = "https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances"
body = {
"name": "[INSTANCE_NAME]",
"machineType": "machineTypes/[MACHINE_TYPE]",
"networkInterfaces": [{
"accessConfigs": [{
"type": "ONE_TO_ONE_NAT",
"name": "External NAT"
}],
"network": "global/networks/default"
}],
"disks": [{
"autoDelete": "true",
"boot": "true",
"type": "PERSISTENT",
"initializeParams": {
"sourceImage": "projects/[IMAGE_PROJECT]/global/images/family/[IMAGE]",
"labels": {
"key": "[LABEL_KEY]",
"value": "[LABEL_VALUE]",
}
}
},
{
"initializeParams": {
"diskSizeGb": "[SIZE_GB]",
"sourceImage":"[IMAGE]"
},
{
"initializeParams": {
"diskSizeGb": "[SIZE_GB]"
}
}]
bodyContentURLEncoded = urllib.urlencode(bodyContent)
resp, content = http.request(uri=url, method="POST", body=dumps(bodyContent), headers=listOfHeaders)
print resp
print content首先,代码是用Python 2.7语法编写的。其次,urllib模块抛出错误:
AttributeError: module 'urllib' has no attribute 'urlencode'第三,即使bodyContent没有抛出这个错误,它也会抛出一个unresolvedReference error,因为它还没有被引用。第四,与listofheaders一样,没有解释http指的是什么或它是什么类型。最后是dumps方法,我不确定Python3中是否有这个方法
https://stackoverflow.com/questions/59562295
复制相似问题