Skip to content

Instantly share code, notes, and snippets.

@ghing
Created April 4, 2011 03:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghing/901110 to your computer and use it in GitHub Desktop.
Save ghing/901110 to your computer and use it in GitHub Desktop.
Simple script to convert a HTTP post data copied from the Tamper Data Firefox extension into Python code that can be used in a script using the mechanize library.
"""
Convert post data copied from the Tamper Data Firefox extension into Python
code for use with mechanize.
Usage: tamper2.py post_data.txt post_data.py br
"""
import sys
import re
def split_long_string(s):
def chunks(l, n):
return [l[i:i+n] for i in range(0, len(l), n)]
return_s = "'%s'" % (s)
if len(s) > 79:
return_s = "(\n"
for chunk in chunks(s, 79):
return_s = return_s + "'%s'\n" % chunk
return_s = return_s + "\n)\n"
return return_s
def print_form_dictionary(dict_name, key, value):
return "%s['%s'] = %s\n" % (dict_name, key, split_long_string(value))
# Main
input_filename = sys.argv[1]
output_filename = sys.argv[2]
dict_name = sys.argv[3]
input_file = open(input_filename)
output_file = open(output_filename, 'w')
for line in input_file.readlines():
if re.match('\S+', line):
parts = line.split('=', 1)
key = ''
value = ''
if len(parts) > 0:
key = parts[0]
if len(parts) > 1:
value = parts[1].strip()
output_file.write(print_form_dictionary(dict_name, key, value))
input_file.close()
output_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment