import requests
import os
import os.path
import sys
import importlib
if os.path.isdir(os.path.join("../../..", "modules")):
module_dir = os.path.join("../../..", "modules")
else:
module_dir = os.path.join("../..", "modules")
module_path = os.path.abspath(module_dir)
if not module_path in sys.path:
sys.path.append(module_path)
import mysocket as sock
importlib.reload(sock)
import util
importlib.reload(util)
import urllib.parse
qstring_value = 'Hellö Wörld@Python?'
urllib.parse.quote(qstring_value)
urllib.parse.quote_plus(qstring_value)
paramD = {'q': 'Python URL encoding', 'as_sitesearch': 'www.urlencoder.io', 'foo': 'Hellö Wörld@Python?'}
urllib.parse.urlencode(paramD)
location = "httpbin.org"
resource = "/get"
url = util.buildURL(resource, location, protocol="https")
print(url)
paramD = {'q': 'Python URL encoding', 'foo': '?@/'}
request = requests.Request("GET", url, params=paramD)
prepared = request.prepare()
print(prepared.path_url)
s = requests.Session()
response = s.send(prepared)
print(response.status_code)
print(response.json())
location = "httpbin.org"
resource = "/get"
url = util.buildURL(resource, location, protocol="https")
paramD = {'q': 'Python URL encoding', 'foo': '?@/'}
response = requests.get(url, params=paramD)
print(response.status_code)
print(response.json())
import json
data_string = """["foo", "bar", {"a": 1, "b": 2}]"""
request_line = 'POST /post?c=foo&d=baz HTTP/1.1\r\n'
host_line = 'Host: httpbin.org\r\n'
one_and_done = 'Connection: close\r\n'
content = 'Content-Type: application/json\r\n'
agent = 'User-Agent: curl/7.71.1\r\n'
empty_line = '\r\n'
request_message = request_line + host_line + \
one_and_done + content + agent + empty_line + data_string
print(request_message)
connection = sock.makeConnection("httpbin.org", 80)
sock.sendString(connection, request_message)
reply = sock.receiveTillClose(connection)
connection.close()
print(reply)
formD = {'q': 'Python URL encoding', 'foo': '?@/'}
data_string = urllib.parse.urlencode(formD)
request_line = 'POST /post?c=foo&d=baz HTTP/1.1\r\n'
host_line = 'Host: httpbin.org\r\n'
one_and_done = 'Connection: close\r\n'
empty_line = '\r\n'
request_message = request_line + host_line + \
one_and_done + empty_line + data_string
print(request_message)
connection = sock.makeConnection("httpbin.org", 80)
sock.sendString(connection, request_message)
reply = sock.receiveTillClose(connection)
connection.close()
print(reply)
!curl -X POST -s -v -T postdata.json "http://httpbin.org/post"
!curl -X POST -s -v -d year=2001 -d newYear='Get+different+year' "http://httpbin.org/post?a=1&b=2"
location = "httpbin.org"
resource = "/post"
url = util.buildURL(resource, location, protocol="https")
paramD = {'a': 'foo', 'b': 'bar'}
formD = {'q': 'Python URL encoding', 'foo': '?@/'}
headerD = {'Accept': 'application/json'}
request = requests.Request("POST", url, params=paramD, data=formD, headers=headerD)
prepared = request.prepare()
print("URI:", prepared.path_url)
print("Request Headers:", prepared.headers)
print("Body:", prepared.body)
response = requests.post(url, params=paramD, data=formD, headers=headerD)
print(response.status_code)
print(response.json())
mystring = 'Hellö Wörld@Python?'
mybytes8 = mystring.encode('UTF-8')
mybytes16 = mystring.encode('UTF-16BE')
len(mystring)
len(mybytes8)
len(mybytes16)
type(mybytes8)
mybytes8.hex()
mybytes8
mybytes8.decode()