如何使文本在python中可点击

时间:2019-02-13 18:45:14

标签: python python-3.x python-2.7 jython-2.7

如何使文本可点击?

类ComplainceServer():         def init (自身,jira_server,用户名,密码,encoding ='utf-8'):             如果jira_server为None:                 错误(“未提供服务器。”

        #print(jira_server)
        self.jira_server = jira_server
        self.username = username
        self.password = password
        self.encoding = encoding

    def checkComplaince(self, appid, toAddress):

        query = "/rest/api/2/search?jql=issuetype = \"Application Security\" AND \"Prod Due Date\" < now()
        request = self._createRequest()
        response = request.get(query, contentType='application/json')
        # Parse result
        if response.status == 200 and action == "warn":
            data = Json.loads(response.response)
            print "#### Issues found"
            issues = {}
            msg = "WARNING: The below tickets are non-complaint in fortify, please fix them or raise exception.\n"
            issue1 = data['issues'][0]['key']
            for item in data['issues']:
                issue = item['key']
                issues[issue] = item['fields']['summary']
                print u"* {0} - {1}".format(self._link(issue), item['fields']['summary'])
                print "\n"
                data = u" {0}  -  {1}".format(self._link(issue), item['fields']['summary'])

                msg += '\n'+ data
            SOCKET_TIMEOUT = 30000 # 30s
            email = SimpleEmail()
            email.setHostName('smtp.com')
            email.setSmtpPort(25)
            email.setSocketConnectionTimeout(SOCKET_TIMEOUT);
            email.setSocketTimeout(SOCKET_TIMEOUT);

            email.setFrom('R@group.com')
            for toAddress in toAddress.split(','):
                email.addTo(toAddress)
            email.setSubject('complaince report')
            email.addHeader('X-Priority', '1')
            email.setMsg(str(msg))
            email.send()

    def _createRequest(self):
        return HttpRequest(self.jira_server, self.username, self.password)

    def _link(self, issue):
        return '[{0}]({1}/browse/{0})'.format(issue, self.jira_server['url'])

这是调用函数。 APPid和toAddress将通过不同的UI传入。

from Complaince import ComplainceServer

jira = ComplainceServer(jiraServer, username, password)

issues = jira.checkComplaince(appid, toAddress)

我希望issueid成为嵌入式链接。

当前电子邮件发送如下:

MT-4353(https://check.com/login/browse/MT-4353) - Site Sc: DM isg_cq5

但是我希望[MT-4353]作为URL https://check.com/login/browse/MT-4353的超链接

1 个答案:

答案 0 :(得分:0)

首先,您需要将电子邮件编码为html。我对您正在使用的库不熟悉,因此无法举一个例子。

我已用html语法替换了您的代码片段,以说明您使用html语法在电子邮件中具有可点击链接的意思。

            msg = "<p>WARNING: The below tickets are non-compliant in fortify, please fix them or raise exception.</p>"
            issue1 = data['issues'][0]['key']
            for item in data['issues']:
                issue = item['key']
                issues[issue] = item['fields']['summary']
                data = u"<a href='{0}'>{1}</a>".format(self._link(issue), item['fields']['summary'])
                msg += '<br />'+ data

将来,请仔细询问您的问题,因为您的标题没有问题并不表示您的实际含义。您还会遇到拼写错误:Compl ia nt

哦,我错过了self._link(issue)的观点,没有返回正确的链接。它返回MT-4353(https://check.com/login/browse/MT-4353),因此您需要提取括号之间的链接部分。我建议使用正则表达式。