In [1]:
import pickle
pickle.dumps([5, 6, 7])


Out[1]:
b'\x80\x03]q\x00(K\x05K\x06K\x07e.'

In [2]:
pickle.loads(b'\x80\x03]q\x00(K\x05K\x06K\x07e.')


Out[2]:
[5, 6, 7]

In [3]:
pickle.loads(b'\x80\x03]q\x00(K\x05K\x06K\x07e.blahblahblah')


Out[3]:
[5, 6, 7]

In [4]:
# how many bytes it processed in order to reload the pickle ?

In [5]:
from io import BytesIO

In [7]:
f = BytesIO(b'\x80\x03]q\x00(K\x05K\x06K\x07e.blahblahblah')

In [9]:
pickle.load(f)


Out[9]:
[5, 6, 7]

In [10]:
f.tell()


Out[10]:
14

In [11]:
f.read()


Out[11]:
b'blahblahblah'

In [12]:
import json

In [13]:
json.dumps([51, 'Namárië!'])


Out[13]:
'[51, "Nam\\u00e1ri\\u00eb!"]'

In [14]:
json.dumps([51, 'Namárië!'], ensure_ascii=False)


Out[14]:
'[51, "Namárië!"]'

In [17]:
json.loads('{"name": "Gaurav", "quest":"Grail"}')


Out[17]:
{'name': 'Gaurav', 'quest': 'Grail'}

In [21]:
json.loads('{"name": "Gaurav", "age":12}')


Out[21]:
{'age': 12, 'name': 'Gaurav'}

In [22]:
# Compresiion

In [23]:
import zlib

In [24]:
data = zlib.compress(b'Python') + b'.' + zlib.compress(b'zlib') + b'.'

In [25]:
data


Out[25]:
b'x\x9c\x0b\xa8,\xc9\xc8\xcf\x03\x00\x08\x97\x02\x83.x\x9c\xab\xca\xc9L\x02\x00\x04d\x01\xb2.'

In [26]:
len(data)


Out[26]:
28

In [27]:
# Imagine that these 28 bytes arrive at their destination in 8-byte packets.

In [28]:
d = zlib.decompressobj()

In [29]:
d.decompress(data[0:8]), d.unused_data


Out[29]:
(b'Pytho', b'')

In [30]:
# as unused_data is empty, there is more data

In [31]:
d.decompress(data[8:16]), d.unused_data


Out[31]:
(b'n', b'.x')

In [33]:
"""Since here you are expecting further compressed data, you will feed the 'x' to a fresh decompress
object to which you can then feed the final 8-byte “packets”"""


Out[33]:
"Since here you are expecting further compressed data, you will feed the 'x' to a fresh decompress\nobject to which you can then feed the final 8-byte “packets”"

In [34]:
d = zlib.decompressobj()

In [35]:
d.decompress(b'x'), d.unused_data


Out[35]:
(b'', b'')

In [36]:
d.decompress(data[16:24]), d.unused_data


Out[36]:
(b'zlib', b'')

In [38]:
d.decompress(data[24:]), d.unused_data


Out[38]:
(b'', b'.')

In [39]:
import socket

In [40]:
s = socket.socket()

In [44]:
try:
    s.connect(('nonexistent.n.c', 80))
except socket.gaierror as e :
    print(e.errno)
    print(e.strerror)
    raise


-2
Name or service not known
---------------------------------------------------------------------------
gaierror                                  Traceback (most recent call last)
<ipython-input-44-ee415639d9bb> in <module>()
      1 try:
----> 2     s.connect(('nonexistent.n.c', 80))
      3 except socket.gaierror as e :
      4     print(e.errno)
      5     print(e.strerror)

gaierror: [Errno -2] Name or service not known

In [45]:
import http.client

In [47]:
connection = http.client.HTTPConnection('nonexistent.foo.bar.com')

In [48]:
connection.request('GET', '/')


---------------------------------------------------------------------------
gaierror                                  Traceback (most recent call last)
<ipython-input-48-0869f11f846a> in <module>()
----> 1 connection.request('GET', '/')

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1237                 encode_chunked=False):
   1238         """Send a complete request to the server."""
-> 1239         self._send_request(method, url, body, headers, encode_chunked)
   1240 
   1241     def _send_request(self, method, url, body, headers, encode_chunked):

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1283             # default charset of iso-8859-1.
   1284             body = _encode(body, 'body')
-> 1285         self.endheaders(body, encode_chunked=encode_chunked)
   1286 
   1287     def getresponse(self):

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
   1232         else:
   1233             raise CannotSendHeader()
-> 1234         self._send_output(message_body, encode_chunked=encode_chunked)
   1235 
   1236     def request(self, method, url, body=None, headers={}, *,

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
   1024         msg = b"\r\n".join(self._buffer)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 
   1028         if message_body is not None:

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in send(self, data)
    962         if self.sock is None:
    963             if self.auto_open:
--> 964                 self.connect()
    965             else:
    966                 raise NotConnected()

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in connect(self)
    934         """Connect to the host and port specified in __init__."""
    935         self.sock = self._create_connection(
--> 936             (self.host,self.port), self.timeout, self.source_address)
    937         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    938 

~/.pyenv/versions/3.6.1/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
    702     host, port = address
    703     err = None
--> 704     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    705         af, socktype, proto, canonname, sa = res
    706         sock = None

~/.pyenv/versions/3.6.1/lib/python3.6/socket.py in getaddrinfo(host, port, family, type, proto, flags)
    741     # and socket type values to enum constants.
    742     addrlist = []
--> 743     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    744         af, socktype, proto, canonname, sa = res
    745         addrlist.append((_intenum_converter(af, AddressFamily),

gaierror: [Errno -5] No address associated with hostname

In [49]:
import urllib.request

In [51]:
urllib.request.urlopen('http://nonexistent.foo.bar.com')


---------------------------------------------------------------------------
gaierror                                  Traceback (most recent call last)
~/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1317                 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318                           encode_chunked=req.has_header('Transfer-encoding'))
   1319             except OSError as err: # timeout error

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1238         """Send a complete request to the server."""
-> 1239         self._send_request(method, url, body, headers, encode_chunked)
   1240 

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1284             body = _encode(body, 'body')
-> 1285         self.endheaders(body, encode_chunked=encode_chunked)
   1286 

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
   1233             raise CannotSendHeader()
-> 1234         self._send_output(message_body, encode_chunked=encode_chunked)
   1235 

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in send(self, data)
    963             if self.auto_open:
--> 964                 self.connect()
    965             else:

~/.pyenv/versions/3.6.1/lib/python3.6/http/client.py in connect(self)
    935         self.sock = self._create_connection(
--> 936             (self.host,self.port), self.timeout, self.source_address)
    937         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

~/.pyenv/versions/3.6.1/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
    703     err = None
--> 704     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    705         af, socktype, proto, canonname, sa = res

~/.pyenv/versions/3.6.1/lib/python3.6/socket.py in getaddrinfo(host, port, family, type, proto, flags)
    742     addrlist = []
--> 743     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    744         af, socktype, proto, canonname, sa = res

gaierror: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
<ipython-input-51-d40eb86e6453> in <module>()
----> 1 urllib.request.urlopen('http://nonexistent.foo.bar.com')

~/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    221     else:
    222         opener = _opener
--> 223     return opener.open(url, data, timeout)
    224 
    225 def install_opener(opener):

~/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
    524             req = meth(req)
    525 
--> 526         response = self._open(req, data)
    527 
    528         # post-process response

~/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py in _open(self, req, data)
    542         protocol = req.type
    543         result = self._call_chain(self.handle_open, protocol, protocol +
--> 544                                   '_open', req)
    545         if result:
    546             return result

~/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    502         for handler in handlers:
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:
    506                 return result

~/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py in http_open(self, req)
   1344 
   1345     def http_open(self, req):
-> 1346         return self.do_open(http.client.HTTPConnection, req)
   1347 
   1348     http_request = AbstractHTTPHandler.do_request_

~/.pyenv/versions/3.6.1/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1318                           encode_chunked=req.has_header('Transfer-encoding'))
   1319             except OSError as err: # timeout error
-> 1320                 raise URLError(err)
   1321             r = h.getresponse()
   1322         except:

URLError: <urlopen error [Errno -5] No address associated with hostname>

In [ ]: