28
文章103
获赞231
评论7.7万+
浏览//通常 100vh 高度将解释调整后的高度,这就是为什么当浏览器的地址栏向下滑动时有时您会看到移动页面变得时髦的原因。
//对于不考虑 vh 单元中的滑动条的浏览器:地址栏的高度在浏览器中不会保持不变,所以我建议不要附加 -50px .
//尝试使用 window.innerheight 属性设置页面的高度(使用 javascript)。
function adjustHeight() {
// alert("宽度:"+window.innerWidth+"高度:"+ window.innerHeight)
var vh = window.innerHeight;
document.getElementById('messageContent').style.height = (vh - 50) + 'px'; // 假设地址栏高度大约为50px
}
window.addEventListener('resize', adjustHeight);
window.addEventListener('load', adjustHeight);
完成最简单的Hello World功能,Flask只需要7行代码,非常简单、方便。
使用Django来完成:
第一步,安装Django,pip install django
第二步,创建工程项目目录,django-admin startprojectmyproject
第三步,创建子应用,python manage.py startappmyapp
第四步,在myapp应用所在目录中的views.py文件中,添加如下代码。
第五步,在myapp应用所在目录中创建urls.py路由文件,并添加如下代码
第六步,在主路由文件中添加路由。
从上面的操作步骤可知,实现同样的一个功能,往往Flask较为简单,代码较少,而Django所涉及到的流程较多,工程项目结构清晰,在大型项目中具有优势。
如果你想搞懂Python web开发WSGI协议原理以及实现过程、或者你想灵活定制组件,完全DIY你的应用、想实现微服务。那么建议你选择Flask。
如果你关注产品的最终交付、想快速开发一个大的应用系统(比如新闻类网站、商城、ERP等)。那么建议你选择Django,你想得到的功能它都有,想不到的功能它也有。
五、如何才能学好框架
1、需要先学习框架的基础知识、基本实现原理
2、结合项目实践,提升编码能力和业务逻辑的理解
3、翻看框架源码,深入理解源码精髓(进阶)
Flask:
1:轻量级web框架,只有一个内核,默认依赖两个外部库:Jinja2 模板引擎和 Werkzeug WSGI 工具集,自由,灵活,可扩展性强,开发者可以根据需求自己造轮子\
2:适用于做小型网站以及web服务的API,开发大型网站无压力,架构需自行设计\
3:与关系型数据库结合不弱于Django,而与非关系型数据库的结合远远优于Django\
\
Django:
1:重量级web框架,功能齐全,提供一站式解决的思路,能让开发者不用在选择应用上花费大量时间\
2:自带ORM(Object-Relational Mapping 对象关联映射)和模板引擎,支持JinJa等非官方模板引擎,灵活度不高\
3:自带ORM使Django和关系型数据库耦合度过高,如果要使用非关系型数据库,需要使用第三方库\
4:自带数据库管理app\
5:成熟、稳定、开发效率高、相对于Flask,Django的整体封闭性比较好,适合做企业级网站的开发\
6:python web框架的先驱,第三方库丰富\
7:上手容易,开发文档详细、完善、资料丰富\
---------------------
(1)Flask
Flask确实很“轻”,不愧是Micro Framework,从Django转向Flask的开发者一定会如此感慨,除非二者均为深入使用过\
Flask自由、灵活,可扩展性强,第三方库的选择面广,开发时可以结合自己最喜欢用的轮子,也能结合最流行最强大的Python库\
入门简单,即便没有多少web开发经验,也能很快做出网站\
非常适用于小型网站\
非常适用于开发web服务的API\
开发大型网站无压力,但代码架构需要自己设计,开发成本取决于开发者的能力和经验\
各方面性能均等于或优于Django\
Django自带的或第三方的好评如潮的功能,Flask上总会找到与之类似第三方库\
Flask灵活开发,Python高手基本都会喜欢Flask,但对Django却可能褒贬不一\
Flask与关系型数据库的配合使用不弱于Django,而其与NoSQL数据库的配合远远优于Django\
Flask比Django更加Pythonic,与Python的philosophy更加吻合\
(2)Django
Django太重了,除了web框架,自带ORM和模板引擎,灵活和自由度不够高\
Django能开发小应用,但总会有“杀鸡焉用牛刀”的感觉\
Django的自带ORM非常优秀,综合评价略高于SQLAlchemy\
Django自带的模板引擎简单好用,但其强大程度和综合评价略低于Jinja\
Django自带ORM也使Django与关系型数据库耦合度过高,如果想使用MongoDB等NoSQL数据,需要选取合适的第三方库,且总感觉Django+SQL才是天生一对的搭配,Django+NoSQL砍掉了Django的半壁江山\
Django目前支持Jinja等非官方模板引擎\
Django自带的数据库管理app好评如潮\
Django非常适合企业级网站的开发:快速、靠谱、稳定\
Django成熟、稳定、完善,但相比于Flask,Django的整体生态相对封闭\
Django是Python web框架的先驱,用户多,第三方库最丰富,最好的Python库,如果不能直接用到Django中,也一定能找到与之对应的移植\
Django上手也比较容易,开发文档详细、完善,相关资料丰富
python manage.py startapp <app_name>,并在Django的项目的settings那个INSTALLED_APPS字典里注册一下就好。
@app.cli.command()
def hello():
click.echo('Hello, xxx!')
flask hello
urlpatterns = [ path('', views.post_list, name='post_list') ]
这个name,相当于Flask的endpoint。意思都是通过反向这个名字,reverse(name)去获得URL。
// 在模板中
<a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1> //Django
{% url_for('") %} // Flask
from flask import request
# 这个request是对每个请求独立的,就像g变量。值得研究底层原理(Flask通过本地线程( thread local) 技术将请求对象在特定
的线程和请求中全局可访问)
@app.route('/hello', methods=['GET', 'POST'])
def hello():
return '<h1>Hello, Flask!</h1>'
Django
from django.views.decorators.http import require_http_methods
@require_http_methods(['GET','POST'])
def login(request):
pass
@app.before_request
def do_something():
pass # 这里的代码会在每个请求处理前执行
Django,通过中间件
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
上面代码就已经是一个简单的 web 应用,从上面的 Hello World 应用的特点来看,一个没什么Python web开发经验的人就可以很快的上手开始撸代码。
django-admin startproject hello_django
django-admin startapp howdy
其目录结构:
默认情况下,Django项目和应用创建好了之后,只包含空的模型和模板文件,而Flask创建项目之后,目录里面没有任何文件,需要我们手动创建,是没有像Django一样组件分离,而对于需要把组件分离开的项目,Flask有blueprints。例如,你可以这样构建你的应用,将与用户有关的功能放在user.py里,把与销售相关的功能放在ecommerce.py里。
Django把一个项目分成各自独立的应用,而Flask认为一个项目应该是一个包含一些视图和模型的单个应用。当然我们也可以在Flask里复制出像Django那样的项目结构。
<!-- view.html -->
<div class="top-bar row">
<div class="col-md-10">
<!-- more top bar things go here -->
</div>
{% if user %}
<div class="col-md-2 whoami">
You are logged in as {{ user.fullname }}
</div>
{% endif %}
</div>
{% for widget in inventory %}
<li><a href="/widget/{{ widget.slug }}/">{{ widget.displayname }}</a></li>
{% endfor %}
Flask默认使用一个受Django启发而发展起来的名为Jinja2的模板,其实,所有上面的Django模板的例子在Jinja2里也是好使的。
<!-- Django -->
<div class="categories">Categories: {{ post.categories|join:", " }}</div>
<!-- Jinja -->
<div class="categories">Categories: {{ post.categories|join(", ") }}</div>
在Jinja的模板语言里,可以把任何数量的参数传给过滤器,因为Jinja像调用一个Python函数的方式来看待它,用圆括号来封装参数。Django使用冒号来分隔过滤器名和参数,这样就只能传递一个参数了。
{% for item in inventory %}
<div class="display-item">{{ item.render() }}</div>
{% else %}
<div class="display-warn">
<h3>No items found</h3>
<p>Try another search, maybe?</p>
</div>
{% endfor %}
{% for item in inventory %}
<div class="display-item">{{ item.render }}</div>
{% empty %}
<div class="display-warn">
<h3>No items found</h3>
<p>Try another search, maybe?</p>
</div>
{% endfor %}
除了上述的语法区别,Flask还提供了很多特有的上下文变量(url_for,get_flashed_messages()等)
----------------------------------------------------------------------------------
Flask框架主要的特点是轻巧,简介,扩展性强;核心就是 Werkzeug(路由模块) ,模板引擎则使用 Jinja2 。
Josn 和xml 的区别:首先他们都是用来数据交互的,其次josn 更加轻量些
Flask中有两种上下文,请求上下文和应用上下文。
上下文:相当于一个容器,他保存了Flask程序运行过程中的一些信息。
g:处理请求时,用于临时存储的对象,每次请求都会重设这个变量。比如:我们可以获取一些临时请求的用户信息。
(通俗来说:g对象和request和session比较像,请求过程中帮助我们创建,请求结束后销毁,是起到一个临时存储的作用)
// 初始化变量
$domain = "www.hankin.cn";
// 批量赋值
$domain = $domain1 = $domain2 = "www.hankin.cn";
Go
// 初始化变量
var domain string = "www.hankin.cn"
// 批量赋值
var domain,domain1,domain2 string = "www.hankin.cn"
// 批量声明赋值
var username,age,local = "zhangsan",13,"BeiJing"
var(
username="zhangsan"
age = 13
local = "BeiJing"
)
define("FOO","something");
GO
// 单独声明
const FOO [string]= something
// 批量声明
const (
USERNAME = "zhangsan"
AGE = 30
)
// 基本输出
echo "www.hankin.cn";
// 格式化输出
printf("my blog %s","www.hankin.cn");
GO
// 基本输出
fmt.Println("www.hankin.cn")
// 格式化输出
fmt.Printf("my blog %s","www.hankin.cn")
// 基本声明
function printString(string $string){
echo $string;
}
// 带返回值
function printString(string $string) : string{
return $string;
}
GO
// 基本声明
func printString(s string){
fmt.Println(s)
}
// 带返回值
func printString(s string) string{
return s
}
namespace Action
use Action
GO
package Action
import "action"
// 初始化
$arr = []
$arr = array()
// 初始化赋值
$arr = [1,2,3]
// 多维数组
$arr = [][]
// 获取值
echo $arr[1]
// 获取数组总数
echo length($arr)
// 获取数组区间
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));
// 设置key=>value
$arr = ["username"=>"zhangsan","age"=>13]
// 删除指定下标
unset($arr[0])
GO 数组 & 切片 (切片是数组的一个View,就例如MySQL的视图一样)
// 初始化
var arr [5]int
// 初始化赋值
arr := [5]int{1, 2, 3, 4, 5}
// 无需声明数组个数
arr := [...]int{1, 2, 3, 4, 5, 6, 7}
// 多维数组
var arr [4][5]bool
// 获取值
fmt.Println(arr[1])
// 获取数组总数
fmt.Println(len(arr))
// 获取数组区间 显而易见,Go对数组的操作更便利直观
a := [...]string{"red","green","blue","yellow","brown"}
fmt.Println(a[1:2])
// 设置key=>value 这里需要使用Map
m := map[string]string{
"username": "zhangsan",
"age" : "13"
}
// 删除指定下标 Go没有删除数组下标的系统方法
arr := arr[1:]
// 删除中间位置的下标 可通过合并的方式去除指定下标
arr := append(arr[:3],arr[4:])
// 基本结构
for($i=0;$i<10;$i++){
echo $i;
}
// 死循环
for($i=0;$i<10;$i++){
echo $i;
$i--
}
// 获取key,value
foreach($arr as $key=>$value){
echo $key,$value
}
GO
// 基本结构
for i := 0; i < 10; i++ {
fmt.Println(i)
}
// 死循环 可见Go写死循环非常方便
for {
fmt.Println("")
}
// 获取key,value
for k, v := range arr {
fmt.Println(k, v)
}
// if
if(true){
}
// switch
switch(true){
case true:
echo true;
break;
}
GO
// if
if true {
}
// switch Go语言的Switch的Case不需要break
switch true {
case true:
fmt.Println(true)
}
// 声明一个类
class City{}
GO
// 声明一个结构体 这里并非混淆公众,是因为Go本身没有类的概念,只是其声明及操作方法与类概念相似
type City struct{}
class User{}
GO
type User struct{}
class User{
public $name;
public $age;
}
GO
type User struct {
username string
age int
}
class User{
public $name;
private $age;
}
GO
// 没有看错,Go中没有保护(protected),变量名首字母大写为public,小写为private
type User struct {
Username string
Age int
}
class User{
public $name;
public $age;
function setName(){
}
function getName(){
}
}
GO
type User struct {
username string
age int
}
func (u User) setName(name string) bool {
u.username = name
return true
}
func (u User) getName() string {
return u.username
}
// php 没有构造方法的话,新建对象无需传参数
new User();
GO
// go 内结构体声明的变量是可选参数,既可传可不传,go既没有类概念,所以也没有构造方法。
User{"zhangsan",15}
$user = new User();
$user->getName();
GO
u := User{"zhangsan",15}
fmt.Println(u.getName())
// 通过composer直接安装,简单易用
composer global require "laravel/installer"
// 创建一个项目
laravel new blog
GO(Beego)
// go自身就有包管理
go get github.com/astaxie/beego
// 创建项目也非常简单
bee api blog
// laravel 的结构这里就不再阐述
| - app
| - bootstrap
| - config
| - database
| - public
| - resources
| - routes
| - storage
| - tests
| - vendor
GO(Beego)
// 显而易见,beego并没有laravel那样过度设计(虽然过度设计并非指目录,
// 但以看目录就知道beego真的没有太多东西)
blog
├── conf
│ └── app.conf
├── controllers
│ └── object.go
│ └── user.go
├── docs
│ └── doc.go
├── main.go
├── models
│ └── object.go
│ └── user.go
├── routers
│ └── router.go
└── tests
└── default_test.go
Route::get('/user', 'UserController@index');
GO(Beego)
// 与laravel的使用方式差不多
// 这里为了统一,路由直接绑定控制器方法只有下列这种
// beego 还提供了注解方式 , 详情见 https://beego.me/docs/mvc/controller/router.md
beego.Router("/user",&UserController{},"get:index")
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* 与模型关联的数据表。
*
* @var string
*/
protected $table = 'user';
}
GO(Beego)
// Beego通过结构体名称作为表名,并且orm操作的所有字段都必须提前声明
package models
import (
"github.com/astaxie/beego/orm"
)
type User struct {
Id int `json:"id"`
Tel string `json:"tel"`
Password string `json:"password"`
Status string `json:"status"`
}
func init() {
orm.RegisterModel(new(User))
}
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* 显示给定用户的概要文件
*
* @param int $id
* @return Response
*/
public function index($id)
{
return view('user.profile', ['user' => User::findOrFail($id)]);
}
}
GO(Beego)
package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
// 这里相当于继承了父类 beegoController
type MemberController struct {
beego.Controller
}
func (c *CityController) Index() {
var results []orm.Params
orm.NewOrm().QueryTable("member").
Values(&results)
c.Data["json"] = results
c.ServeJSON()
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<title>瀑布流</title>
<style>
:root {--color1: #a3d9a5;--color2: #f6b93b;--color3: #38ada9;--color4: #e55039;--color5: #1e3799;--color6: #6a89cc;--color7: #f8c291;--color8: #b71540;}
.lists .item:nth-child(odd) {background-color: var(--color1);}
.lists .item:nth-child(even) {background-color: var(--color2);}
.lists .item:nth-child(3n) {background-color: var(--color3);}
.lists .item:nth-child(4n) {background-color: var(--color4);}
.lists .item:nth-child(5n) {background-color: var(--color5);}
.lists .item:nth-child(6n) {background-color: var(--color6);}
.lists .item:nth-child(7n) {background-color: var(--color7);}
.lists .item:nth-child(8n) {background-color: var(--color8);}
.lists {width: 1200px;margin: 0 auto;column-count: 4;column-gap: 10px;transition: column-count 0.3s ease-in-out;}
.lists .item {height: 160px;margin-bottom: 10px;break-inside: avoid;color: #000000;text-align: center;font-size: 30px;transition: height 0.3s ease-in-out;}
/* 更多的高度和选择器组合 */
.lists .item:nth-child(3n+1) { height: 200px; }
.lists .item:nth-child(4n+1) { height: 250px; }
.lists .item:nth-child(5n+1) { height: 180px; }
.lists .item:nth-child(6n+1) { height: 280px; }
.lists .item:nth-child(7n+1) { height: 220px; }
.lists .item:nth-child(8n+1) { height: 300px; }
.lists .item:nth-child(9n+1) { height: 230px; }
.lists .item:nth-child(10n+1) { height: 260px; }
@media (max-width: 992px) {
.lists {width: auto;padding: 10px;box-sizing: border-box;column-count: 3;column-gap: 10px;}
.lists .item {break-inside: avoid;width: auto;text-align: center;margin-bottom: 10px;}
/* 在较小屏幕下适应不同的高度 */
.lists .item:nth-child(3n+1) { height: 220px; }
.lists .item:nth-child(4n+1) { height: 180px; }
.lists .item:nth-child(5n+1) { height: 250px; }
.lists .item:nth-child(6n+1) { height: 200px; }
}
@media (max-width: 768px) {
.lists {width: auto;padding: 10px;box-sizing: border-box;column-count: 2;column-gap: 10px;}
.lists .item {break-inside: avoid;width: auto;height: 200px;line-height: 200px;text-align: center;margin-bottom: 10px;}
.lists .item:nth-child(2n+1) {height: 240px;}
.lists .item:nth-child(3n+1) {height: 320px;}
}
</style>
</head>
<body>
<div class="lists"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div><div class="item">5</div><div class="item">6</div><div class="item">7</div><div class="item">8</div><div class="item">9</div><div class="item">10</div><div class="item">11</div><div class="item">12</div><div class="item">13</div><div class="item">14</div><div class="item">15</div><div class="item">16</div>
</div>
</body>
</html>
<?php
// +----------------------------------------------------------------------
// | 作者 hankin [ http://www.hankin.cn ]
// +----------------------------------------------------------------------
// | 版权所有 2024-06-27 13:06 hankin
// +----------------------------------------------------------------------
// | 官方网站: http://www.hankin.cn
// +----------------------------------------------------------------------
?>
<?php
$gzh_app_id = "公众号appid";//公众号appid
$gzh_app_secret = "公众号appsecret";//公众号appsecret
?>
<?php if(isset($_GET['redirectUrl']) && !empty($_GET['redirectUrl'])):?>
<!-- 获取跳转url地址 -->
<script type="text/javascript">localStorage.setItem('redirectUrl','<?= $_GET['redirectUrl'];?>')</script>
<?php endif;?>
<?php if(isset($_GET['type']) && !empty($_GET['type'])):
//发起授权
$redirectUrl = $_SERVER['REQUEST_SCHEME'] . '://'. $_SERVER['SERVER_NAME'] . '/' . $_GET['type'] . '.php';
//获取公众号授权拿到code
$codeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$gzh_app_id."&redirect_uri=".urlencode($redirectUrl)."&response_type=code&scope=snsapi_userinfo&forcePopup=true&forceSnapShot=true&state=STATE#wechat_redirect";
//header("location: ".$codeUrl);
?>
<script type="text/javascript">
//跳转code页面地址
setTimeout(function(){
window.location.href = '<?= $codeUrl;?>'
},500)
</script>
<?php endif;?>
<?php if(isset($_GET['code']) && !empty($_GET['code'])):
//根据code获取微信用户信息
$data = getSimpleUserInfo($_GET['code'], $gzh_app_id, $gzh_app_secret);
?>
<?php
//微信用户信息解析
$ret['userinfo']['openid'] = $data['openid'] ?? '';//微信用户唯一标识 openid
$ret['userinfo']['unionid'] = $data['unionid'] ?? '';//微信用户唯一标识 unionid
$ret['userinfo']['nickname'] = $data['nickname']?? '';//微信用户昵称
$ret['userinfo']['avatar'] = $data['headimgurl']?? '';//微信用户头像
?>
<script type="text/javascript">
//跳转回调地址
window.location.href = localStorage.getItem('redirectUrl')+'?data=<?= urlencode(json_encode($ret));?>'
//JSON.parse(decodeURIComponent(window.location.href.split('?data=')[1]))
</script>
<?php endif;?>
<?php
function getSimpleUserInfo($code, $WECHAT_APPID, $WECHAT_APPSECRET)
{
if (empty($code)) return array();
//通过code换取网页授权access_token
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $WECHAT_APPID . '&secret=' . $WECHAT_APPSECRET . '&code=' . $code . '&grant_type=authorization_code';
$access_token_json = https_request($access_token_url);
$access_token_array = json_decode($access_token_json, true);
$openid = isset($access_token_array['openid']) ? $access_token_array['openid'] : '';
$access_token = isset($access_token_array['access_token']) ? $access_token_array['access_token'] : '';
$userinfo_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
$userinfo_json = https_request($userinfo_url);
$userinfo_array = json_decode($userinfo_json, true);
return $userinfo_array;
}
//请求接口
function https_request($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {
return 'ERROR ' . curl_error($curl);
}
curl_close($curl);
return $data;
}
/**
* 生成AJAX结果
* @param $resultCode
* @param null $message
* @param null $data
* @return array
*/
function generateAjaxResult($resultCode, $message = NULL, $data = NULL)
{
exit(json_encode([
'code' => $resultCode,
'msg' => $message,
'result' => $data,
]));
}
/**
* AJAX成功返回数据
* @param null $data
* @return array
*/
function ajaxSuccess($data = NULL,$message = NULL)
{
$result = generateAjaxResult(
200,
$message,
$data
);
return $result;
}
/**
* AJAX错误返回数据
* @param null $message
* @param int $resultCode
* @param null $data
* @return array
*/
function ajaxError($message = NULL, $resultCode = 1000, $data = NULL)
{
$result = generateAjaxResult(
$resultCode,
$message,
$data
);
return $result;
}
?>
TenAPI(免费):https://docs.tenapi.cn/
百度AI:http://ai.baidu.com/
微博:https://open.weibo.com/wiki/API
讯飞:https://www.xfyun.cn/
APISpace:https://www.apispace.com/
聚合:https://www.juhe.cn/
旷视人脸:https://www.faceplusplus.com.cn/
融云:https://www.rongcloud.cn/
京东云:https://wx.jdcloud.com/api
高德:https://lbs.amap.com/
腾讯云产品API中心:https://cloud.tencent.com/api
阿里API市场:https://market.aliyun.com/products/56956004/
百度:https://apis.baidu.com/
def get_sign(Pm):
#这里是加密需要的字段
arg0 = Pm['appKey']
arg1 = Pm['utdid'] + "&"
arg1 = arg1 + Pm['uid']+ "&"
arg1 = arg1 + Pm['reqbiz-ext']+ "&"
arg1 = arg1 + Pm['appKey']+ "&"
arg1 = arg1 + Pm['datamd5']+ "&"
arg1 = arg1 + Pm['t']+ "&"
arg1 = arg1 + Pm['api']+ "&"
arg1 = arg1 + Pm['v']+ "&"
arg1 = arg1 + Pm['sid']+ "&"
arg1 = arg1 + Pm['ttid']+ "&"
arg1 = arg1 + Pm['deviceId']+ "&"
arg1 = arg1 + Pm['lat']+ "&"
arg1 = arg1 + Pm['lng']+ "&"
arg1 = arg1 + Pm['ext']+ "&"
arg1 = arg1 + Pm['x-features']+ "&"
arg1 = arg1 + Pm['routerId']+ "&"
arg1 = arg1 + Pm['placeId']+ "&"
arg1 = arg1 + Pm['openBiz']+ "&"
arg1 = arg1 + Pm['miniAppKey']+ "&"
arg1 = arg1 + Pm['reqAppKey']+ "&"
arg1 = arg1 + Pm['act']+ "&"
arg1 = arg1 + Pm['openBizData']
arg2 = Pm['api']
arg3 = "pageName="+ Pm['pageName'] + "&pageId=" + Pm['pageId']
sign = xianyu.get70102(arg0,arg1,arg2,arg3)
ret = eval(str(sign))
Pm.update(ret)
return ret
#在这里定义自己的字段
def build_Pm(api,v,data):
Pm = {}
Pm['x-app-ver'] = "7.1.60"
Pm['utdid'] = "YIJLR2Y/7fgDAMOPtQCGzfRz"
Pm['uid'] = ""#登录后才有
Pm['reqbiz-ext'] = ""
Pm['appKey'] = "21407387"
Pm['datamd5'] = hashlib.md5(data.encode("utf-8")).hexdigest() if data != "" else ""
Pm['t'] = str(int(time.time()))
Pm['api'] = api
Pm['v'] = v
Pm['sid'] = ""#登录后才有
Pm['ttid'] = "36137321407327@fleamarket_android_7.1.60"
Pm['deviceId'] = "AlKfW_V2tm3mJ3AYHwUErKPkq41dPGN2vXWlskFJDb2s"
Pm['lat'] = "0"
Pm['lng'] = "0"
Pm['ext'] = "openappkey=DEFAULT_AUTH"
Pm['x-features'] = "27"
Pm['routerId'] = ""
Pm['placeId'] = ""
Pm['openBiz'] = ""
Pm['miniAppKey'] = ""
Pm['reqAppKey'] = ""
Pm['act'] = ""
Pm['openBizData'] = ""
Pm['pageName'] = ""
Pm['pageId'] = ""
Pm['x-sgext'] = ""
Pm['x-umt'] = ""
Pm['x-mini-wua'] = ""
Pm['x-sign'] = ""
Pm['x-pv'] = "6.3"
Pm['x-bx-version'] = "6.5.24"
Pm['User-Agent'] = "MTOPSDK/3.1.1.7+(Android;7.1.2;HUAWEI;VOG-AL00)"
Pm['Cookie'] = ""
Pm['f-refer'] = "mtop"
sign = get_sign(Pm)
return Pm
def xianyu_post(url,api,v,data,_headers = {}):
Pm = build_Pm(api,v,data)
headers = {
"x-extdata": Pm['ext'],
"x-features": Pm['x-features'],
"x-sgext": quote_plus(Pm['x-sgext']),
"umid": quote_plus(Pm['x-umt']),
"User-Agent": quote_plus(Pm['User-Agent']),
"x-ttid": quote_plus(Pm['ttid']),
"content-type": "application/x-www-form-urlencoded;charset=UTF-8",
"a-orange-q":"appKey="+Pm['appKey']+"&appVersion="+Pm['x-app-ver']+"&clientAppIndexVersion=1120210930160801265&clientVersionIndexVersion=0",
"x-appkey": Pm['appKey'],
"x-mini-wua": quote_plus(Pm['x-mini-wua']),
"x-nq" : "WIFI",
"x-nettype": "WIFI",
"first_open" : "0",
"x-c-traceid": Pm['ttid'] + Pm['t'] +"332000317813",
"x-app-conf-v": "0",
"x-pv": Pm['x-pv'],
"x-bx-version": Pm['x-bx-version'],
"x-t": Pm['t'],
"x-app-ver": Pm['x-app-ver'],
"f-refer": Pm['f-refer'],
"Cookie" : Pm['Cookie'],
"x-sid": Pm['sid'],
"x-utdid": Pm['utdid'],
"x-umt": quote_plus(Pm['x-umt']),
"x-devid": Pm['deviceId'],
"x-sign": quote_plus(Pm['x-sign']),
"x-location": quote_plus("{0},{1}".format(Pm['lng'], Pm['lat'])),
"x-page-name": Pm['pageName'],
"x-page-url": quote_plus(Pm['pageId']),
"x-uid": Pm['uid']
}
headers.update(_headers)
url = url + "/" + api + "/" + v + "/"
postdata = "data=" + quote_plus(data)
ret = requests.post(url, data=postdata, headers=headers)
return ret
url = "http://acs.m.taobao.com/gw"
api = "mtop.taobao.idle.local.flow.plat.container"
v = "1.0"
data = '{"productId":"2","unionKey":"online_local_concept_container"}'
ret = xianyu_post(url,api,v,data)
print(ret.text)
暂无数据