python实现发送form-data数据的方法详解


Posted in Python onSeptember 27, 2019

本文实例讲述了python实现发送form-data数据的方法。分享给大家供大家参考,具体如下:

源代码

-----------------------------279361243530614
Content-Disposition: form-data; name="parent_dir"

/
-----------------------------279361243530614
Content-Disposition: form-data; name="file"; filename="f5.txt"
Content-Type: text/plain

#NodeAddress
import sys
import pycontrol.pycontrol as pc
import time
import csv
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
b = pc.BIGIP(
hostname = '10.4.170.160',
username = 'zjtlcb',
password = 'xxx',
fromurl = True,
  wsdls=['LocalLB.NodeAddress'])
node = b.LocalLB.NodeAddress
print node
print dir(node)
ipaddress='10.4.161.148'
print dir(node)
print node.get_session_enabled_state(node_addresses=[ipaddress])
print node.get_session_status(node_addresses=[ipaddress])
print node.get_monitor_status(node_addresses=[ipaddress])
print node.get_object_status(node_addresses=[ipaddress])
print node.set_monitor_state(node_addresses=[ipaddress],states=['STATE_FORCED_DISABLED'])
print node.get_session_enabled_state(node_addresses=[ipaddress])
print node.get_session_status(node_addresses=[ipaddress])
print node.get_monitor_status(node_addresses=[ipaddress])
print node.get_object_status(node_addresses=[ipaddress])
print node.set_monitor_state(node_addresses=[ipaddress],states=['STATE_ENABLED'])
print node.get_session_enabled_state(node_addresses=[ipaddress])
print node.get_session_status(node_addresses=[ipaddress])
print node.get_monitor_status(node_addresses=[ipaddress])

-----------------------------279361243530614--

# !/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import httplib
import re
import requests
import StringIO
import time
import sys
import json
import re
import random
from urllib import quote
from requests_toolbelt import MultipartEncoder
import time
s = requests.session()
username='015208@zjtlcb.com'
password='newja01'
myurl='http://10.4.48.2:8000/accounts/login?next=/'
headers = {
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0'
   }
response = s.get(myurl, headers=headers,timeout=10)
a= response.content
#<form action="" method="post" class="con"><input type='hidden' name='csrfmiddlewaretoken' value='ZWnUDf5XVX0kagjIoJLKyU8UdO8KBGFn' />
#p=re.compile('.*?<token>(.*?)</token>*')
p=re.compile('.*<form.*value=\'(.*?)\'.*\s+/>',flags=re.S)
m=p.match(a)
print m
token= m.group(1)
print token
myurl='http://10.4.48.2:8000/accounts/login/?next=/'
headers = {
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0'
   }
data={'csrfmiddlewaretoken':token,'login':username,'password':password,'next':'/'}
response = s.post(myurl, data=data, headers=headers,timeout=10)
print response.content
u1='http://10.4.48.2:8000/api2/repos/?type=mine&_=1553493865054'
a= s.get(u1).content
print a
print type(a)
b=json.loads(a)
print b
print type(b)
for i in range(len(b)):
  print str(b[i]).decode('unicode-escape')
  if b[i]['name'] == 'perl':
    uploadid= b[i]['id']
print uploadid
ctime=time.time()
ctime=int(ctime)
u2='http://10.4.48.2:8000/ajax/repo/%s/file_op_url/?op_type=upload&path=%s&_=%s' %(uploadid,'%2F',ctime)
print u2
headers = {
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
  'X-Requested-With':'XMLHttpRequest'
   }
response=s.get(u2,headers=headers)
b= response.content
print type(b)
print b
c=json.loads(b)
d= c['url']
u3=d
headers = {
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
  'X-Requested-With':'XMLHttpRequest'
   }
m=MultipartEncoder(
  fields={
    "parent_dir":'/',
    "name":'file',
    "filename":'abc.txt',
    'file':('abc.txt',open('abc.txt','rb'),'text/plain')
  }
)
headers['Content-Type']=m.content_type
response=s.post(u3,headers=headers,data=m,timeout=20)
print response.content

 python实现发送form-data数据的方法详解

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
使用Python的Django框架实现事务交易管理的教程
Apr 20 Python
Python中的time模块与datetime模块用法总结
Jun 30 Python
python中WSGI是什么,Python应用WSGI详解
Nov 24 Python
PyCharm设置SSH远程调试的方法
Jul 17 Python
解决python3读取Python2存储的pickle文件问题
Oct 25 Python
Python正则表达式和元字符详解
Nov 29 Python
Python----数据预处理代码实例
Mar 20 Python
Python匿名函数及应用示例
Apr 09 Python
Python实现密码薄文件读写操作
Dec 16 Python
Python面向对象之私有属性和私有方法应用案例分析
Dec 31 Python
python连接mysql有哪些方法
Jun 24 Python
Python下划线5种含义代码实例解析
Jul 10 Python
PyCharm更改字体和界面样式的方法步骤
Sep 27 #Python
Pycharm 字体大小调整设置的方法实现
Sep 27 #Python
python3 写一个WAV音频文件播放器的代码
Sep 27 #Python
简单瞅瞅Python vars()内置函数的实现
Sep 27 #Python
Python 获取项目根路径的代码
Sep 27 #Python
Python Pandas对缺失值的处理方法
Sep 27 #Python
Python 可变类型和不可变类型及引用过程解析
Sep 27 #Python
You might like
PHP中str_replace函数使用小结
2008/10/11 PHP
如何优雅的使用 laravel 的 validator验证方法
2018/11/11 PHP
Cookie跨域问题解决方案代码示例
2020/11/24 PHP
动手学习无线电
2021/03/10 无线电
jQuery ui 利用 datepicker插件实现开始日期(minDate)和结束日期(maxDate)
2014/05/22 Javascript
详解Javascript 装载和执行
2014/11/17 Javascript
javascript实现博客园页面右下角返回顶部按钮
2015/02/22 Javascript
详解JavaScript的while循环的使用
2015/06/03 Javascript
JavaScript来实现打开链接页面的简单实例
2016/06/02 Javascript
常用的几个JQuery代码片段
2017/03/13 Javascript
angular内置provider之$compileProvider详解
2017/09/27 Javascript
bootstrap日期插件daterangepicker使用详解
2017/10/19 Javascript
vue+swiper实现侧滑菜单效果
2017/12/28 Javascript
JS实现的透明度渐变动画效果示例
2018/04/28 Javascript
Vue组件全局注册实现警告框的实例详解
2018/06/11 Javascript
jquery+css实现Tab栏切换的代码实例
2019/05/14 jQuery
微信小程序动态添加和删除组件的现实
2020/02/28 Javascript
Element-ui 自带的两种远程搜索(模糊查询)用法讲解
2021/01/29 Javascript
Python BeautifulSoup中文乱码问题的2种解决方法
2014/04/22 Python
Python实现读取文件最后n行的方法
2017/02/23 Python
pandas.dataframe按行索引表达式选取方法
2018/10/30 Python
将tensorflow.Variable中的某些元素取出组成一个新的矩阵示例
2020/01/04 Python
降低python版本的操作方法
2020/09/11 Python
Python 利用Entrez库筛选下载PubMed文献摘要的示例
2020/11/24 Python
微软香港官网及网上商店:Microsoft HK
2016/09/01 全球购物
下列程序在32位linux或unix中的结果是什么
2014/03/25 面试题
建筑工程毕业生自我鉴定
2014/01/14 职场文书
办公室文员自荐书
2014/02/03 职场文书
2014年医生工作总结
2014/11/21 职场文书
个性与发展自我评价
2015/03/06 职场文书
2015大学党建带团建工作总结
2015/07/23 职场文书
Oracle 数据仓库ETL技术之多表插入语句的示例详解
2021/04/12 Oracle
Go 自定义package包设置与导入操作
2021/05/06 Golang
浅谈redis整数集为什么不能降级
2021/07/25 Redis
javascript对象3个属性特征
2021/11/17 Javascript
Windows 11上手初体验:任务栏和开始菜单等迎来大改
2021/11/21 数码科技