Skip to content

Commit 4180cf8

Browse files
committed
code_snippet: [Python] HTTP GET/POST 请求
1 parent f6031f5 commit 4180cf8

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

code_snippet.html

+1-1
Large diffs are not rendered by default.

code_snippet.md

+83
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,89 @@ hello
13411341
apple
13421342
```
13431343

1344+
## [Python] HTTP GET/POST 请求
1345+
1346+
### urllib2
1347+
1348+
```python
1349+
# coding = utf-8
1350+
import urllib2
1351+
import json
1352+
import sys
1353+
reload(sys)
1354+
sys.setdefaultencoding('utf-8')
1355+
1356+
1357+
def get(url):
1358+
resp = urllib2.urlopen(url=url, timeout=10)
1359+
return resp.read()
1360+
1361+
1362+
def post(url, data):
1363+
resp = urllib2.urlopen(url=url, data=data, timeout=10)
1364+
return resp.read()
1365+
1366+
1367+
if __name__ == '__main__':
1368+
data1 = get('http://45.77.40.154:9999/admin_stat/product')
1369+
print(data1)
1370+
1371+
payload = {
1372+
'product': 'ahihi'
1373+
}
1374+
data2 = post('http://45.77.40.154:9999/admin_stat/category',
1375+
json.dumps(payload))
1376+
print(data2)
1377+
```
1378+
1379+
**输出**
1380+
1381+
```basic
1382+
{"message": "success", "data": [{"name": "\u5934\u6761\u4ea7\u54c1", "value": "topnews"}, {"name": "\u5c0f\u8bf4\u6f2b\u753b", "value": "ahihi"}]}
1383+
1384+
{"message": "success", "data": [{"description": "total product data", "children": [], "name": "\u4ea7\u54c1\u603b\u4f53\u6570\u636e",
1385+
"data_cat": "total_product_data,"}]}
1386+
```
1387+
1388+
### requests
1389+
1390+
```python
1391+
# coding = utf-8
1392+
import requests
1393+
import sys
1394+
import json
1395+
reload(sys)
1396+
sys.setdefaultencoding('utf-8')
1397+
1398+
if __name__ == '__main__':
1399+
r = requests.get('http://45.77.40.154:9999/admin_stat/product')
1400+
print(r.content)
1401+
1402+
payload = {
1403+
'product': 'ahihi'
1404+
}
1405+
r = requests.post('http://45.77.40.154:9999/admin_stat/category',
1406+
json.dumps(payload))
1407+
print('URL: ', r.url)
1408+
print('HEADERS: ', r.headers)
1409+
print('Content-Type: ', r.headers.get('content-type'))
1410+
print('CODE: ', r.status_code)
1411+
print(r.content)
1412+
```
1413+
1414+
**输出**
1415+
1416+
```basic
1417+
{"message": "success", "data": [{"name": "\u5934\u6761\u4ea7\u54c1", "value": "topnews"}, {"name": "\u5c0f\u8bf4\u6f2b\u753b", "value": "ahihi"}]}
1418+
1419+
('URL: ', u'http://45.77.40.154:9999/admin_stat/category')
1420+
('HEADERS: ', {'Date': 'Sun, 27 May 2018 09:50:04 GMT', 'Content-Length': '171', 'Content-Type': 'application/json', 'Server': 'Werkzeug/0.12.2 Python/2.7.5'})
1421+
('Content-Type: ', 'application/json')
1422+
('CODE: ', 200)
1423+
{"message": "success", "data": [{"description": "total product data", "children": [], "name": "\u4ea7\u54c1\u603b\u4f53\u6570\u636e",
1424+
"data_cat": "total_product_data,"}]}
1425+
```
1426+
13441427
## [Python] Flask RESTful
13451428

13461429
> **环境:**Python 2.7.14/Flask 0.11.1/Flask-RESTful 0.3.5/Flask-SQLAlchemy 2.2

config.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,14 @@ imap jj <ESC>
154154
"editor.insertSpaces": true,
155155
"editor.tabSize": 4,
156156
"files.trimTrailingWhitespace": true,
157-
"files.autoSave": "afterDelay"
157+
"files.autoSave": "afterDelay",
158+
"editor.formatOnSave": true,
159+
"editor.formatOnPaste": true,
160+
"window.zoomLevel": 2,
161+
"explorer.confirmDelete": true,
162+
"editor.detectIndentation": true,
163+
"team.showWelcomeMessage": false,
164+
"python.linting.flake8Enabled": true
158165
}
159166
```
160167

0 commit comments

Comments
 (0)