Python re.search函数正则表达式问题

时间:2016-09-08 23:27:40

标签: python regex

我有以下代码:

#!/usr/bin/python

import time, uuid, hmac, hashlib, base64, json
import urllib3
import certifi
import datetime
import requests
import re
from datetime import datetime

http = urllib3.PoolManager(
    cert_reqs='CERT_REQUIRED', # Force certificate check.
    ca_certs=certifi.where(),  # Path to the Certifi bundle.
)

#Get the status response from pritunl api
BASE_URL = 'https://www.vpn.trimble.cloud:443'
API_TOKEN = 'gvwrfQZQPryTbX3l03AQMwTyaE0aFywE'
API_SECRET = 'B0vZp5dDyOrshW1pmFFjAnIUyeGtFy9y'
LOG_PATH = '/var/log/developer_vpn/'
def auth_request(method, path, headers=None, data=None):
    auth_timestamp = str(int(time.time()))
    auth_nonce = uuid.uuid4().hex
    auth_string = '&'.join([API_TOKEN, auth_timestamp, auth_nonce,
        method.upper(), path] + ([data] if data else []))
    auth_signature = base64.b64encode(hmac.new(
        API_SECRET, auth_string, hashlib.sha256).digest())
    auth_headers = {
        'Auth-Token': API_TOKEN,
        'Auth-Timestamp': auth_timestamp,
        'Auth-Nonce': auth_nonce,
        'Auth-Signature': auth_signature,
    }
    if headers:
        auth_headers.update(headers)
    return http.request(method, BASE_URL + path, headers=auth_headers, body=data)


response1 = auth_request('GET',
  '/server',
)
if response1.status == 200:
    pritunlServResponse = (json.loads(response1.data))
    #print pritunlServResponse
    #print response1.data

    Name = [y['name'] for y in pritunlServResponse]
    Server_id = [x['id'] for x in pritunlServResponse]

    for srv_name, srv_id in zip(Name, Server_id):
        response2 = auth_request('GET',
        '/server/' + srv_id + '/output',
        )
        pritunlServResponse2 = (json.loads(response2.data))
        py_pritunlServResponse2 = pritunlServResponse2['output']

        print("value of srv_id: ", srv_id, "\n")
        print("value of srv_name: ", srv_name, "\n") 

        logfile = open(LOG_PATH + srv_name +'_vpn_out.log', 'w')
        for log in py_pritunlServResponse2:
            if re.search(r'(?!52\.39\.62\.8)', log):
                logfile.write("%s\n" % log)

        logfile.close()

else:
    raise SystemExit

此代码使用身份验证访问网站(地址已被编辑),抓取一些用JSON格式化的文本,并从输出中解析两个值:" srv_name"和" srv_id"。然后此代码使用" srv_id"构造其他HTTP请求以从服务器获取日志文件。然后它抓取日志文件 - 每个文件一个" srv_id"并使用从" srv_name"获得的值命名它们。并将它们保存在本地系统上。

我想在将文件写入本地系统之前进行一些额外的grep样式处理。具体来说,我想排除任何完全包含" 52.39.62.8"从写作。当我运行上面的代码时,看起来正在处理正则表达式,因为我仍然看到" 52.39.62.8"在我的输出文件中。

2 个答案:

答案 0 :(得分:1)

如果IP地址始终是特定字符的两侧,例如:(52.39.62.8):,则可以使用in来确定包含:

if '(52.39.62.8):' not in log:
    logfile.write(log + '\n')

答案 1 :(得分:0)

re.search(r'(?!52\.39\.62\.8)', log)

你匹配任何空字符串,后面没有ip地址 - 每个字符串都匹配,因为这将匹配任何字符串的结尾。

反转您的逻辑并仅在IP地址的re.search返回None时将该行输出到日志。

if re.search(r'(?<!\d)52\.39\.62\.8(?!\d)', log) is None:
    logfile.write("%s\n" % log)

请注意,这还包括它自己的负面后视和前瞻断言,以确保在ip地址之前或之后没有数字。