Skip to content

Instantly share code, notes, and snippets.

@laixintao
Created August 25, 2017 09:24
Show Gist options
  • Save laixintao/22b0d29cc48b5481c1d416cff51d2108 to your computer and use it in GitHub Desktop.
Save laixintao/22b0d29cc48b5481c1d416cff51d2108 to your computer and use it in GitHub Desktop.
format chrome formdata to requests data.
# -*- coding: utf-8 -*-
"""
$ python formdata.py "typeId=2&enTableName=SYJ_SXQYHMD_SYJ&searchName=&personField=QYMC&page=1&pageSize=10"
[
('typeId', '2'),
('enTableName', 'SYJ_SXQYHMD_SYJ'),
('searchName', ''),
('personField', 'QYMC'),
('page', '1'),
('pageSize', '10'),
]
"""
from __future__ import unicode_literals
import sys
def main():
if not sys.argv[1]:
print("Ussage: formdata typeId=2&test=string")
form_source = sys.argv[1]
pairs = form_source.split('&')
output_template = "[\n{}\n]"
fields = []
for pair in pairs:
key, value = pair.split('=')
fields.append(" ('{}', '{}'),".format(key, value))
print output_template.format('\n'.join(fields))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment