基于Python 实现对C文件指定字符串接口爬取

1、python脚本实现
2、pycharm环境变量配置
3、win环境调试
4、Linux 环境调试

# !usr/bin/env Python
# coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import threading

import re
import sys
import copy

import fileinput
import os
import getopt
import stat
import re
from ctypes import *
import subprocess

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

def analyzecode(file_path,origin_path):
    log_line = 0
    text2_log_line = 0
    key = 0
    linux_path = file_path.replace('\\', '/')
    file = open(linux_path, 'r+')    
    try:
        text_lines = file.readlines()
        text_lines2 = copy.deepcopy(text_lines)
        for i, line in enumerate(text_lines):
            if 'log_id_msg' in line:
                log_line = i
                print(log_line)
                text2_log_line = log_line
                replace_log_line = log_line
                while text_lines[log_line].find(";", 0, len(text_lines[log_line]) - 1) == -1:
                        text_lines2[text2_log_line] += text_lines[log_line+1]
                        log_line += 1
                        key += 1
                pattern = re.compile(r'"(?:.|\n)*?"')
                log_str = pattern.findall(text_lines2[text2_log_line])
                log_str = " ".join(log_str)
		delete = re.compile(r'"')
		new_str = delete.sub('', log_str)
		c = str(new_str).encode('ascii')
                print'insert string is: ', c
                strinfo = re.compile('0xdeadbeef')
                if (text_lines[replace_log_line].find(";", 0, len(text_lines[replace_log_line]) - 1)) != -1:
                    if text_lines[replace_log_line].find("0xdeadbeef", 0, len(text_lines[replace_log_line]) - 1) > 0:
 			os.chdir(origin_path)
                	sql_id = lte.lte_string_key_insert(c)
               		print("sql_id=%d\n", sql_id)
               		sql_id_to_str = '%d' % sql_id
                        text_lines[replace_log_line] = strinfo.sub(sql_id_to_str, text_lines[replace_log_line])
                else:
                    while text_lines[replace_log_line].find(";", 0, len(text_lines[replace_log_line]) - 1) == -1:
                        if text_lines[replace_log_line].find("0xdeadbeef", 0, len(text_lines[replace_log_line]) - 1) > 0:
			    os.chdir(origin_path)
                            sql_id = lte.lte_string_key_insert(c)
                            print("sql_id=%d\n", sql_id)
                            sql_id_to_str = '%d' % sql_id
                            text_lines[replace_log_line] = strinfo.sub(sql_id_to_str, text_lines[replace_log_line])
                            print(text_lines[replace_log_line])
                        replace_log_line += 1

        file.close()
        file = open(linux_path, 'w')
        file.writelines(text_lines)
    finally:
        file.close()

def argument_parser(argv):
   inputfile = ''
   outputfile = ''
   try:
      opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
   except getopt.GetoptError:
      print('test.py -i <inputfile> -o <outputfile>')
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print('test.py -i <inputfile> -o <outputfile>')
         sys.exit()
      elif opt in ("-i", "--ifile"):
         inputfile = arg
	 origin_path = getpwd()
         linux_fmt = re.compile(r'(?:.|\n)*?[/]')
         temp_str = linux_fmt.findall(sys.argv[2])
         #print(temp_str)
         if temp_str:
             temp = "".join(temp_str)
             #print('linux_str: ', temp)
	     pattern = re.compile(temp)
             inputfile = pattern.sub('', sys.argv[2])
             os.chdir(temp)
	     sys.argv[1] = os.getcwd()
         else: 
             sys.argv[1] = getpwd()
         #print('current folder: ', sys.argv[1])
	 
         findfile(sys.argv[1],inputfile,origin_path)
      elif opt in ("-o", "--ofile"):
         outputfile = arg

def findfile(path, name,origin_path):
    file_path = ""
    full_path = ""
    for root, dirs, files in os.walk(path, topdown=False):
        if name in files:
            full_path = os.path.join(path, root, name)            
            file_path = os.path.normpath(os.path.abspath(full_path))             
    analyzecode(file_path,origin_path)

def getpwd():
    pwd = sys.path[0]
    if os.path.isfile(pwd):
        pwd = os.path.dirname(pwd)
    #print(pwd)
    return pwd
def current_path(): 
    print("changed working directory ") 
    print(os.getcwd())  
if __name__ == '__main__':
    lte = CDLL('./libstring_key.so')
    argument_parser(sys.argv[1:])


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

echo-niuben

你的鼓励是我创作的巨大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值