This is a demonstration of how to use requests in Python. In this example, we are using requests to access a file using the GitHub API. The resulting JSON object contains a base64-encoded attribute (content), which is decoded, tuned into a list of lines. Lastly, we select some lines of interest.

This example is used in our work on software metrics and the literal include service (for writing projects).


In [1]:
import requests
import base64

In [2]:
r = requests.get("https://api.github.com/repos/gkthiruvathukal/st-hec/contents/hydra/dataserver.py")

In [3]:
print(r.status_code)


200

In [4]:
r.json().keys()


Out[4]:
dict_keys(['content', 'encoding', 'type', '_links', 'url', 'path', 'html_url', 'download_url', 'size', 'git_url', 'sha', 'name'])

In [5]:
b64data = r.json().get('content')

lines = base64.b64decode(b64data).decode("utf-8").split('\n')
selected_lines = lines[10:30]
print("\n".join(selected_lines))


#   (at your option) any later version.
#
#   The Hydra Filesystem is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with the Hydra Filesystem; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#######################################################################################



from common import network_services,packet_builder,packet_types,synchronized
from common.dispatch import Dispatcher
from common.rep_db import *
from common import plugin

This shows how to base64 encode text. b64encode() expects bytes and returns bytes (b).


In [6]:
text = "How to encode some text"
bytes = text.encode("UTF-8")
encoded = base64.b64encode(bytes)
encoded


Out[6]:
b'SG93IHRvIGVuY29kZSBzb21lIHRleHQ='

To get the string representation of bytes, use decode().


In [7]:
encoded.decode("UTF-8")


Out[7]:
'SG93IHRvIGVuY29kZSBzb21lIHRleHQ='