• python http 上传文件


    文章目录


    import random
    import requests
    from requests_toolbelt.multipart.encoder import MultipartEncoder
    
    url = 'http://ip:port/email'
    data = MultipartEncoder(
        fields={
            'receiverId': 'xxxx@163.com',
            'mailSubject': 'mailSubject',
            'content': 'content',
            'fileList': ('file_name', open('/opt/app/data/xxx.out', 'rb'), 'application/octet-stream')
        },
        boundary='-----------------' + str(random.random()*1e9)
    )
    headers = {'content-type': 'multipart/form-data'}
    r = requests.post(url, data=data, headers={'content-type': data.content_type})
    print(r.text)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    改进质量

    
    /*
     * Copyright (C) Igor Sysoev
     * Copyright (C) Nginx, Inc.
     */
    
    
    #include 
    #include 
    #include 
    
    
    ngx_int_t
    ngx_write_channel(ngx_socket_t s, ngx_channel_t *ch, size_t size,
        ngx_log_t *log)
    {
        ssize_t             n;
        ngx_err_t           err;
        struct iovec        iov[1];
        struct msghdr       msg;
    
    #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
    
        union {
            struct cmsghdr  cm;
            char            space[CMSG_SPACE(sizeof(int))];
        } cmsg;
    
        if (ch->fd == -1) {
            msg.msg_control = NULL;
            msg.msg_controllen = 0;
    
        } else {
            msg.msg_control = (caddr_t) &cmsg;
            msg.msg_controllen = sizeof(cmsg);
    
            ngx_memzero(&cmsg, sizeof(cmsg));
    
            cmsg.cm.cmsg_len = CMSG_LEN(sizeof(int));
            cmsg.cm.cmsg_level = SOL_SOCKET;
            cmsg.cm.cmsg_type = SCM_RIGHTS;
    
            /*
             * We have to use ngx_memcpy() instead of simple
             *   *(int *) CMSG_DATA(&cmsg.cm) = ch->fd;
             * because some gcc 4.4 with -O2/3/s optimization issues the warning:
             *   dereferencing type-punned pointer will break strict-aliasing rules
             *
             * Fortunately, gcc with -O1 compiles this ngx_memcpy()
             * in the same simple assignment as in the code above
             */
    
            ngx_memcpy(CMSG_DATA(&cmsg.cm), &ch->fd, sizeof(int));
        }
    
        msg.msg_flags = 0;
    
    #else
    
        if (ch->fd == -1) {
            msg.msg_accrights = NULL;
            msg.msg_accrightslen = 0;
    
        } else {
            msg.msg_accrights = (caddr_t) &ch->fd;
            msg.msg_accrightslen = sizeof(int);
        }
    
    #endif
    
        iov[0].iov_base = (char *) ch;
        iov[0].iov_len = size;
    
        msg.msg_name = NULL;
        msg.msg_namelen = 0;
        msg.msg_iov = iov;
        msg.msg_iovlen = 1;
    
        n = sendmsg(s, &msg, 0);
    
        if (n == -1) {
            err = ngx_errno;
            if (err == NGX_EAGAIN) {
                return NGX_AGAIN;
            }
    
            ngx_log_error(NGX_LOG_ALERT, log, err, "sendmsg() failed");
            return NGX_ERROR;
        }
    
        return NGX_OK;
    }
    
    
    ngx_int_t
    ngx_read_channel(ngx_socket_t s, ngx_channel_t *ch, size_t size, ngx_log_t *log)
    {
        ssize_t             n;
        ngx_err_t           err;
        struct iovec        iov[1];
        struct msghdr       msg;
    
    #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
        union {
            struct cmsghdr  cm;
            char            space[CMSG_SPACE(sizeof(int))];
        } cmsg;
    #else
        int                 fd;
    #endif
    
        iov[0].iov_base = (char *) ch;
        iov[0].iov_len = size;
    
        msg.msg_name = NULL;
        msg.msg_namelen = 0;
        msg.msg_iov = iov;
        msg.msg_iovlen = 1;
    
    #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
        msg.msg_control = (caddr_t) &cmsg;
        msg.msg_controllen = sizeof(cmsg);
    #else
        msg.msg_accrights = (caddr_t) &fd;
        msg.msg_accrightslen = sizeof(int);
    #endif
    
        n = recvmsg(s, &msg, 0);
    
        if (n == -1) {
            err = ngx_errno;
            if (err == NGX_EAGAIN) {
                return NGX_AGAIN;
            }
    
            ngx_log_error(NGX_LOG_ALERT, log, err, "recvmsg() failed");
            return NGX_ERROR;
        }
    
        if (n == 0) {
            ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "recvmsg() returned zero");
            return NGX_ERROR;
        }
    
        if ((size_t) n < sizeof(ngx_channel_t)) {
            ngx_log_error(NGX_LOG_ALERT, log, 0,
                          "recvmsg() returned not enough data: %z", n);
            return NGX_ERROR;
        }
    
    #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
    
        if (ch->command == NGX_CMD_OPEN_CHANNEL) {
    
            if (cmsg.cm.cmsg_len < (socklen_t) CMSG_LEN(sizeof(int))) {
                ngx_log_error(NGX_LOG_ALERT, log, 0,
                              "recvmsg() returned too small ancillary data");
                return NGX_ERROR;
            }
    
            if (cmsg.cm.cmsg_level != SOL_SOCKET || cmsg.cm.cmsg_type != SCM_RIGHTS)
            {
                ngx_log_error(NGX_LOG_ALERT, log, 0,
                              "recvmsg() returned invalid ancillary data "
                              "level %d or type %d",
                              cmsg.cm.cmsg_level, cmsg.cm.cmsg_type);
                return NGX_ERROR;
            }
    
            /* ch->fd = *(int *) CMSG_DATA(&cmsg.cm); */
    
            ngx_memcpy(&ch->fd, CMSG_DATA(&cmsg.cm), sizeof(int));
        }
    
        if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
            ngx_log_error(NGX_LOG_ALERT, log, 0,
                          "recvmsg() truncated data");
        }
    
    #else
    
        if (ch->command == NGX_CMD_OPEN_CHANNEL) {
            if (msg.msg_accrightslen != sizeof(int)) {
                ngx_log_error(NGX_LOG_ALERT, log, 0,
                              "recvmsg() returned no ancillary data");
                return NGX_ERROR;
            }
    
            ch->fd = fd;
        }
    
    #endif
    
        return n;
    }
    
    
    ngx_int_t
    ngx_add_channel_event(ngx_cycle_t *cycle, ngx_fd_t fd, ngx_int_t event,
        ngx_event_handler_pt handler)
    {
        ngx_event_t       *ev, *rev, *wev;
        ngx_connection_t  *c;
    
        c = ngx_get_connection(fd, cycle->log);
    
        if (c == NULL) {
            return NGX_ERROR;
        }
    
        c->pool = cycle->pool;
    
        rev = c->read;
        wev = c->write;
    
        rev->log = cycle->log;
        wev->log = cycle->log;
    
        rev->channel = 1;
        wev->channel = 1;
    
        ev = (event == NGX_READ_EVENT) ? rev : wev;
    
        ev->handler = handler;
    
        if (ngx_add_conn && (ngx_event_flags & NGX_USE_EPOLL_EVENT) == 0) {
            if (ngx_add_conn(c) == NGX_ERROR) {
                ngx_free_connection(c);
                return NGX_ERROR;
            }
    
        } else {
            if (ngx_add_event(ev, event, 0) == NGX_ERROR) {
                ngx_free_connection(c);
                return NGX_ERROR;
            }
        }
    
        return NGX_OK;
    }
    
    
    void
    ngx_close_channel(ngx_fd_t *fd, ngx_log_t *log)
    {
        if (close(fd[0]) == -1) {
            ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "close() channel failed");
        }
    
        if (close(fd[1]) == -1) {
            ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "close() channel failed");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
  • 相关阅读:
    GitOps 工具 Argo CD 实战教程
    【单链表,循环链表和双向链表的时间效率比较,顺序表和链表的比较,有序表的合并------用顺序表实现,用链表实现】
    IOT跨平台组件设计方案
    浅谈在操控器类中,为何要通过osgGA::CameraManipulator的逆矩阵改变视点位置
    通过浏览器打开某个应用程序
    [Python数据可视化] Plotly:交互式数据可视化的强大工具
    matlab创建矩阵、理解三维矩阵
    什么是安全运营中心(SOC),应该了解什么
    JVM概述
    HBase的逻辑结构与物理结构
  • 原文地址:https://blog.csdn.net/wwq921220/article/details/132816885