Python and Golang upload images
来源:互联网 发布:headfirst python pdf 编辑:程序博客网 时间:2024/06/07 12:12
Python模拟HTTP上传图片
#utf-8import threadingfrom time import ctimefrom poster.encode import multipart_encodefrom poster.streaminghttp import register_openersimport urllib2i=0for i in range(1): register_openers() datagen, headers = multipart_encode({"file": open("pian1.jpg", "rb")}) request = urllib2.Request("http://test.seeunsee.cn/jsb/wm-test/api/check.php", datagen, headers) i+=1 print urllib2.urlopen(request).read(),i
Golang模拟HTTP上传图片
package mainimport ( "bytes" "fmt" "io" "log" "mime/multipart" "net/http" "os" "path/filepath")// Creates a new file upload http request with optional extra paramsfunc newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) { file, err := os.Open(path) if err != nil { return nil, err } defer file.Close() body := &bytes.Buffer{} writer := multipart.NewWriter(body) part, err := writer.CreateFormFile(paramName, filepath.Base(path)) if err != nil { return nil, err } _, err = io.Copy(part, file) for key, val := range params { _ = writer.WriteField(key, val) } err = writer.Close() if err != nil { return nil, err } req, err := http.NewRequest("POST", uri, body) req.Header.Set("Content-Type", writer.FormDataContentType()) return req, err}func main() { path, _ := os.Getwd()//获取当前绝对文件路径 path += "/pian.jpg" extraParams := map[string]string{ "id": "WU_FILE_0", "name": "pian.jpg", "type": "image/jpeg", "lastModifiedDate": "Mon Apr 17 2017 15:31:08 GMT+0800 (CST)", "size": "241281", } request, err := newfileUploadRequest("http://test.seeunsee.cn/jsb/wm-test/api/check.php", extraParams, "file", path) if err != nil { log.Fatal(err) } client := &http.Client{} resp, err := client.Do(request) if err != nil { log.Fatal(err) } else { body := &bytes.Buffer{} _, err := body.ReadFrom(resp.Body) if err != nil { log.Fatal(err) } resp.Body.Close() //fmt.Println(resp.StatusCode) //fmt.Println(resp.Header) fmt.Println(body) }}
0 0
- Python and Golang upload images
- Use the JQuery To upload images and create thumbnail
- python upload file and form
- Golang Multipart File Upload Example
- QML Color and Images
- Docker Images and Registry
- Converting between ROS images and OpenCV images
- abap upload and download
- Java_ftp_file upload and download
- python Upload Ftp
- Python Flask Upload Files
- An Introduction to Interactive Programming in Python (Part 2) week 6 (Classes and Tied images)
- Detecting Skin in Images & Video Using Python and OpenCV皮肤检测
- App Icon and Launch Images
- Resize images and save thumbnails
- SE78 PIC UPLOAD AND DOWNLOAD
- Retrofit upload files and text
- picture upload view and html2canvas
- lspci 命令使用
- OutOfMemoryError异常
- 学习资源收集
- 史上最全的HTML基础教程(收藏)
- unity导出工程与iOS工程合并
- Python and Golang upload images
- python起步之认识缩进
- 【SQL】IN、EXISTS和表连接三者的效率比较
- Springz中对象的Scope
- android theme属性
- Web端轻松实现音视频聊天通话
- Android IM即时通信开发总结及代码
- Makefile经典教程(掌握这些足够)
- imx6从YOCTO中提取交叉编译器