TypeError:必须是str,而不是int error

时间:2017-11-14 06:05:44

标签: python python-3.x

我正在尝试将数据从CGI脚本发送到C服务器,以便将数据写入XML文件。

Client.cgi:

#include <string>
#include <vector>
#include <algorithm>

int main() {
    std::vector<std::string> vs = {"123", "456", "789"};
    std::vector<int> vi;
    std::transform(vs.begin(), vs.end(),
        std:back_inserter(vi),
        [](const auto& element) { return std::stoi(element); });
// 'vi' now contains the integers 123, 456, 789
}

通过命令行传递参数,如

import cgi, cgitb
import os
import sys
import argparse
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings()
import sys
import socket


form = cgi.FieldStorage()


#--PARSING THE ARGUMENTS
#--To be used to get arguments from Command Line or Php Script
parser = argparse.ArgumentParser(description ='parser script')
parser.add_argument('--serverId', required=True, help='Server ID to find device info')
parser.add_argument('--name', required=True, help='Name Of the Policy')
parser.add_argument('--host', default='any', help='Host of the IP Address')
parser.add_argument('--port', default='any', help='Port used')
parser.add_argument('--proto', default='any', help='Protocol used')
parser.add_argument('--username', default='any', help='Username')
parser.add_argument('--password', default='any', help='Password')
parser.add_argument('--key', default='any', help='')
parser.add_argument('--companyName', default='any', help='Name of the Company')
parser.add_argument('--officeAddress', default='any', help='Address of the Company')
parser.add_argument('--state', default='any', help='State')
parser.add_argument('--country', default='any', help='Country')
parser.add_argument('--ladmin', default='any', help='Local Admin')
parser.add_argument('--phone', default='any', help='Phone Number')
parser.add_argument('--mobile', default='any', help='Mobile Number')
parser.add_argument('--email', default='any', help='Email Address')
parser.add_argument('--designation', default='any', help='Designation of the concerned')
parser.add_argument('--rManager', default='any', help='Reporting Manager of the concerned Person')

args = parser.parse_args()


#--Associating variables to parsed arguments
serverId = str(args.serverId)
name = str(args.name)
host = str(args.host)
port = str(args.port)
proto = str(args.proto)
user = str(args.username)
password = str(args.password)
key = str(args.key)
company = str(args.companyName)
officeAddr = str(args.officeAddress)
state = str(args.state)
country = str(args.country)
ladmin = str(args.ladmin)
phone = str(args.phone)
mobile = str(args.mobile)
email = str(args.email)
designation = str(args.designation)
reportingMgr = str(args.rManager)

def client_program():
    host = socket.gethostname()  # as both code is running on same pc
    port = 8888  # socket server port number

    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # instantiate
    client_socket.connect((host, port))  # connect to the server

    sum = serverId+";"+name+";"+host+";"+port+";"+proto+";"+user+";"+password+";"+key+";"+company+";"+officeAddr+";"+state+";"+country+";"+ladmin+";"+phone+";"+mobile+";"+email+";"+designation+";"+reportingMgr

    client_socket.sendall(sum.encode('utf-8'))
    data = client_socket.recv(1024)
    text = str(data)

    print('received:' +text)
    print('closing socket')
    client_socket.close()

if __name__ == '__main__':
    client_program()

我得到一个追踪调用如下

python dscr.cgi --serverId PA124 --name Devanshu

我意识到这只是一个愚蠢的错误,但我无法找出什么。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您的port变量是int,您必须先将其转换为str

您实际上在某个时刻将port转换为str,但稍后将其分配给函数范围内的int:port = 8888
也许是一些调试代码的残余?

#                                       **here**
    sum = serverId+";"+name+";"+host+";"+str(port)+";"+proto+";"+user+";"+password+";"+key+";"+company+";"+officeAddr+";"+state+";"+country+";"+ladmin+";"+phone+";"+mobile+";"+email+";"+designation+";"+reportingMgr

在@COLDSPEED建议中,您可以对所有这些参数进行分组并在其上调用join

arguments =  [serverId, 
              name,
              host,
              str(port),
              proto,
              user,
              password,
              key,
              company,
              officeAddr,
              state,
              country,
              ladmin,
              phone,
              mobile,
              email,
              designation,
              reportingMgr]

print(';'.join(arguments))

答案 1 :(得分:0)

替换以下行

port = 8888  # socket server port number

port = '8888'  # socket server port number

或者你可以在形成连接字符串之前键入强制转换,因为字符串连接操作只能用另一个字符串来完成,即替换

sum = serverId+";"+name+";"+host+";"+port+";"+proto+";"+user+";"+password+";"+key+";"+company+";"+officeAddr+";"+state+";"+country+";"+ladmin+";"+phone+";"+mobile+";"+email+";"+designation+";"+reportingMgr

sum = serverId+";"+name+";"+host+";"+str(port)+";"+proto+";"+user+";"+password+";"+key+";"+company+";"+officeAddr+";"+state+";"+country+";"+ladmin+";"+phone+";"+mobile+";"+email+";"+designation+";"+reportingMgr