-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathupload_or_post.py
160 lines (113 loc) · 4.42 KB
/
upload_or_post.py
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
from socketify import App
###
# We always recommend check res.aborted in async operations
###
def upload(res, req):
print(f"Posted to {req.get_url()}")
def on_data(res, chunk, is_end):
print(f"Got chunk of data with length {len(chunk)}, is_end: {is_end}")
if is_end:
res.cork_end("Thanks for the data!")
res.on_data(on_data)
async def upload_chunks(res, req):
print(f"Posted to {req.get_url()}")
# await all the data, returns received chunks if fail (most likely fail is aborted requests)
data = await res.get_data()
print(f"Got {len(data.getvalue())} bytes of data!")
# We respond when we are done
res.cork_end("Thanks for the data!")
async def upload_json(res, req):
print(f"Posted to {req.get_url()}")
# await all the data and parses as json, returns None if fail
info = await res.get_json()
print(info)
# We respond when we are done
res.cork_end("Thanks for the data!")
async def upload_text(res, req):
print(f"Posted to {req.get_url()}")
# await all the data and decode as text, returns None if fail
text = await res.get_text() # first parameter is the encoding (default utf-8)
print(f"Your text is {text}")
# We respond when we are done
res.cork_end("Thanks for the data!")
async def upload_urlencoded(res, req):
print(f"Posted to {req.get_url()}")
# await all the data and decode as application/x-www-form-urlencoded, returns None if fails
form = (
await res.get_form_urlencoded()
) # first parameter is the encoding (default utf-8)
print(f"Your form is {form}")
# We respond when we are done
res.cork_end("Thanks for the data!")
async def upload_multiple(res, req):
print(f"Posted to {req.get_url()}")
content_type = req.get_header("content-type")
# we can check the Content-Type to accept multiple formats
if content_type == "application/json":
data = await res.get_json()
elif content_type == "application/x-www-form-urlencoded":
data = await res.get_form_urlencoded()
else:
data = await res.get_text()
print(f"Your data is {data}")
# We respond when we are done
res.cork_end("Thanks for the data!")
def upload_formdata(res, req):
# using streaming_form_data package for parsing
from streaming_form_data import StreamingFormDataParser
from streaming_form_data.targets import ValueTarget, FileTarget
print(f"Posted to {req.get_url()}")
parser = StreamingFormDataParser(headers=req.get_headers())
name = ValueTarget()
parser.register('name', name)
file = FileTarget('/tmp/file')
file2 = FileTarget('/tmp/file2')
parser.register('file', file)
parser.register('file2', file2)
def on_data(res, chunk, is_end):
parser.data_received(chunk)
if is_end:
res.cork(on_finish)
def on_finish(res):
print(name.value)
print(file.multipart_filename)
print(file.multipart_content_type)
print(file2.multipart_filename)
print(file2.multipart_content_type)
res.end("Thanks for the data!")
res.on_data(on_data)
async def upload_formhelper(res, req):
# using streaming_form_data package for parsing + helper
from streaming_form_data import StreamingFormDataParser
from streaming_form_data.targets import ValueTarget, FileTarget
from helpers.form_data import get_formdata
print(f"Posted to {req.get_url()}")
parser = StreamingFormDataParser(headers=req.get_headers())
name = ValueTarget()
parser.register('name', name)
file = FileTarget('/tmp/file')
file2 = FileTarget('/tmp/file2')
parser.register('file', file)
parser.register('file2', file2)
await get_formdata(res, parser)
print(name.value)
print(file.multipart_filename)
print(file.multipart_content_type)
print(file2.multipart_filename)
print(file2.multipart_content_type)
res.cork_end("Thanks for the data!")
app = App()
app.post("/", upload)
app.post("/chunks", upload_chunks)
app.post("/json", upload_json)
app.post("/text", upload_text)
app.post("/urlencoded", upload_urlencoded)
app.post("/multiple", upload_multiple)
app.post("/formdata", upload_formdata)
app.post("/formdata2", upload_formhelper)
app.any("/*", lambda res, _: res.write_status(404).end("Not Found"))
app.listen(
3000,
lambda config: print("Listening on port http://localhost:%d now\n" % config.port),
)
app.run()