python - mitmproxy replace entire HTML tag -


well....looks first post stackoverflow. totally stumped , wasted hours working on little project , i'm ready give up. can me.

i've been working mitmproxy. i'm writing python script replace body tag in http response custom content when user loads page.

from libmproxy import controller, proxy import re import os import sys  class bodyinjector(controller.master):     def __init__(self, server):         controller.master.__init__(self, server)      def run(self):         try:             return controller.master.run(self)         except keyboardinterrupt:             self.shutdown()      def handle_request(self, msg):         print 'new request detected.'         if 'accept-encoding' in msg.headers:             msg.headers["accept-encoding"] = ["none"]             print 'changing encoding.'         msg.reply()      def handle_response(self, msg):         if 'content-type' in msg.headers:             if msg.headers["content-type"] == ["text/html"]:                 print 'webpage detected.  injecting response.'                 if msg.content:                     c = msg.replace('/<body itemscope itemtype=\"http:\/\/schema\.org\/webpage\">.*?<\/body>/', '<body><p>replacement text.</p></body>')                     print 'num replacements:  ' + str(c)                     if c > 0:                         print 'injection successful.'                     else:                         print 'injection unsuccessful.'         msg.reply()  def main(argv):     config = proxy.proxyconfig(cacert = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem"))     server = proxy.proxyserver(config, 1080)     print 'starting body replace server'     m = bodyinjector(server)     m.run()  main(sys.argv) 

i think problem line:

c = msg.replace('/<body itemscope itemtype=\"http:\/\/schema\.org\/webpage\">.*?<\/body>/', '<body><p>replacement text.</p></body>') 

i've tested regular expression online regex tester. think problem html multi-line. need somehow set re.dotall flag i'm not sure if that's possible msg.replace method developer wrote.

here link documentation of replace method.

does have idea how this? i'm desperate. :'(


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -