本文整理汇总了Python中urequests.post方法的典型用法代码示例。如果您正苦于以下问题:Python urequests.post方法的具体用法?Python urequests.post怎么用?Python urequests.post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urequests
的用法示例。
在下文中一共展示了urequests.post方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def send(self, dataframe: DataFrame):
"""
Submit telemetry data using HTTP POST request
:param dataframe:
"""
log.info('Sending HTTP request to %s', self.uri)
log.info('HTTP payload: %s', dataframe.payload_out)
import urequests
response = urequests.post(self.uri, data=dataframe.payload_out.encode(), headers={'Content-Type': self.content_type})
if response.status_code in [200, 201]:
return True
else:
message = 'HTTP request failed: {} {}\n{}'.format(response.status_code, response.reason, response.content)
raise TelemetryTransportError(message)
示例2: token
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def token(self):
print('token: build jwt')
# prepare a JWT
builder = JWTBuilder()
builder.service_account(self._email)
builder.scope(self._scope)
builder.key(self._key)
jwt = builder.build()
print('token: jwt is done')
type = 'urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer'
body = 'grant_type=%s&assertion=%s' % (type, jwt)
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post('https://www.googleapis.com/oauth2/v4/token',
data=body,
headers=headers)
if not response:
print('token: no response received')
return response.json()['access_token']
示例3: append_values
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def append_values(self, values):
print('spreadsheet: send: %s' % values)
token = self._sa.token()
url = self._url_template % (self._id, self._range, self._url_params)
values.insert(0, ntp.time())
data = {'values': [ values ]}
headers = {}
headers['Content-Type'] = 'application/json'
headers['Authorization'] = 'Bearer %s' % token
response = requests.post(url, json=data, headers=headers)
if not response:
print('spreadsheet: no response received')
print('spreadsheet: response:')
print(response.text)
示例4: download_nametag
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def download_nametag(self):
payload = {
"name": self.name,
"org": self.org,
"ext": self.filename.split('.')[-1] # file extension
}
headers = {'Content-Type':'application/json'}
data = (json.dumps(payload)).encode()
r = urequests.post(self.url, data=data, headers=headers)
if r.status_code == 200:
ugfx.display_image(0, 0, bytearray(r.content))
if not self.IS_TEST:
# Write it as a file
f = open(self.filename, 'wb')
f.write(bytearray(r.content))
f.close()
else:
print(r.text)
raise Exception(r.text)
r.close()
示例5: send_to_influx
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def send_to_influx(datapoints):
data = '\n'.join(d.to_influx() for d in datapoints)
# mean_data = DataPoint.mean(datapoints)
# data = mean_data.to_influx(include_timestamp=False)
print('sending data\n{}'.format(data))
influx_url = 'http://rpi.local:8086/write?db=mydb'
success = False
number_of_retries = 3
while not success and number_of_retries > 0:
try:
urequests.post(influx_url, data=data)
success = True
except OSError as e:
print('network error: {}'.format(e.errno))
number_of_retries -= 1
pass
return success
示例6: create_datastream
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def create_datastream(self, json, headers=None):
headers = self.set_headers(headers)
response = urequests.post(STREAMS_URI + "inventory/", headers=headers, json=json)
return self.check_response_code(response)
示例7: add_datapoint
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def add_datapoint(self, stream_id, value, headers=None):
headers = self.set_headers(headers)
response = urequests.post(STREAMS_URI + "history/", headers=headers, json={"stream_id": stream_id, "value": value})
return self.check_response_code(response)
示例8: push
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def push(URL, msg):
to_post = json.dumps(msg)
response = requests.post(firebaseURL(URL), data=to_post)
if response.status_code != 200:
raise Exception(response.text)
示例9: send_instapush_notification
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def send_instapush_notification(app_id, app_secret, event, trackers):
import urequests, json
url = 'https://api.instapush.im/v1/post'
headers = {'x-instapush-appid': app_id, 'x-instapush-appsecret': app_secret, 'Content-Type': "application/json"}
data = '{"event": "' + event + '", "trackers": ' + json.dumps(trackers) + '}'
resp = urequests.post(url, data=data, headers=headers)
return resp.json()
# @timed_function
示例10: test_urequests_basic
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def test_urequests_basic(httpserver_ipv4):
"""
A basic test showing that the "urequests" module works properly.
"""
httpserver = httpserver_ipv4
# Define HTTP conversation details.
request_data = {'hello': 'world'}
response_data = {'status': 'ok'}
# Mock HTTP conversation.
httpserver.expect_request("/api/data").respond_with_json(response_data)
# Get URL to be invoked.
url = httpserver.url_for("/api/data")
# Invoke HTTP request.
import urequests
response = urequests.post(url, json=request_data)
# Proof that worked.
# Investigate the real response.
assert response.content == json.dumps(response_data, indent=4).encode()
# Investigate within the HTTP server.
assert len(httpserver.log) == 1, "pytest-httpserver didn't capture any request"
request, response = httpserver.log[0]
assert request.get_data() == json.dumps(request_data).encode()
示例11: test_redirect_cpython_requests
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def test_redirect_cpython_requests(httpserver_ipv4):
"""
Proof that HTTP redirects work.
This is the reference implementation using the CPython "requests" module.
"""
httpserver = httpserver_ipv4
# Define HTTP conversation details.
request_data = {'hello': 'world'}
response_data = {'status': 'ok'}
# Mock HTTP conversation.
def handler(request: werkzeug.Request):
response = werkzeug.Response(status=307)
response.headers.add('Location', '/api/v2/data')
return response
httpserver.expect_request("/api/v1/data").respond_with_handler(handler)
httpserver.expect_request("/api/v2/data").respond_with_json(response_data)
# Get URL to be invoked.
url = httpserver.url_for("/api/v1/data")
# Invoke HTTP request.
requests.post(url, json=request_data)
# Proof that worked.
request, response = httpserver.log[0]
assert request.get_data() == json.dumps(request_data).encode()
assert response.status_code == 307
assert response.get_data() == b''
request, response = httpserver.log[1]
assert request.get_data() == json.dumps(request_data).encode()
assert response.status_code == 200
assert response.get_data() == json.dumps(response_data, indent=4).encode()
示例12: init_iotf
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def init_iotf(self):
# Show
self.view_manager.open_status_box()
# Check for network availability
self.view_manager.set_status_text('Waiting for network')
if not util.wait_network():
self.view_manager.set_status_text('Cannot connect WiFi')
raise Exception('Cannot connect WiFi')
sta_if = network.WLAN(network.STA_IF)
mac_addr = ''.join('{:02X}'.format(c) for c in sta_if.config('mac'))
orgId = iotfcfg.orgId
deviceType = iotfcfg.deviceType
deviceId = iotfcfg.deviceId if hasattr(iotfcfg, 'deviceId') else mac_addr
user = 'use-token-auth'
authToken = iotfcfg.authToken
clientID = 'd:' + orgId + ':' + deviceType + ':' + deviceId
broker = orgId + '.messaging.internetofthings.ibmcloud.com'
# Check for device registration
url = 'https://hongjs-nodered.mybluemix.net/api/badge2018/type/{}/register'.format(deviceType)
payload = {
'deviceId': deviceId,
'authToken': authToken,
'deviceInfo': {},
'groups': [],
'location': {},
'metadata': {}
}
headers = {'token':'helloiot'}
r = urequests.post(url, json=payload, headers=headers)
if r.status_code == 201:
print('OK')
elif r.status_code == 409:
print('Already Exists')
else:
print(r.text)
raise Exception(r.text)
r.close()
self.deviceId = deviceId
self.mqtt = simple.MQTTClient(clientID, broker, user=user, password=authToken, ssl=True)
self.mqtt.set_callback(self.sub_cb)
self.mqtt.connect()
self.mqtt.subscribe(self.COMMAND_TOPIC_RPS)
# Close Popup
self.view_manager.close_status_box()
示例13: test_redirect_urequests
# 需要导入模块: import urequests [as 别名]
# 或者: from urequests import post [as 别名]
def test_redirect_urequests(httpserver_ipv4):
"""
Proof that HTTP redirects work, now based on urequests.
"""
httpserver = httpserver_ipv4
# Define HTTP conversation details.
request_data = {'hello': 'world'}
response_data = {'status': 'ok'}
# Mock HTTP conversation.
def handler(request: werkzeug.Request):
response = werkzeug.Response(status=307)
response.headers.add('Location', '/api/v2/data')
return response
httpserver.expect_request("/api/v1/data").respond_with_handler(handler)
httpserver.expect_request("/api/v2/data").respond_with_json(response_data)
# Get URL to be invoked.
url = httpserver.url_for("/api/v1/data")
# Invoke HTTP request.
import urequests
response = urequests.post(url, json=request_data)
# Proof that worked.
# Investigate the real response.
assert response.content == json.dumps(response_data, indent=4).encode()
# Investigate within the HTTP server.
request, response = httpserver.log[0]
assert request.get_data() == json.dumps(request_data).encode()
assert response.status_code == 307
assert response.get_data() == b''
request, response = httpserver.log[1]
assert request.get_data() == json.dumps(request_data).encode()
assert response.status_code == 200
assert response.get_data() == json.dumps(response_data, indent=4).encode()