Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Python Networking

You're reading from   Learning Python Networking Utilize Python 3 to get network applications up and running quickly and easily

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher
ISBN-13 9781784396008
Length 320 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. Network Programming and Python 2. HTTP and Working with the Web FREE CHAPTER 3. APIs in Action 4. Engaging with E-mails 5. Interacting with Remote Systems 6. IP and DNS 7. Programming with Sockets 8. Client and Server Applications 9. Applications for the Web A. Working with Wireshark Index

Requests with urllib


We have already seen some examples of HTTP exchanges while discussing the RFC downloaders in Chapter 1, Network Programming and Python. The urllib package is broken into several submodules for dealing with the different tasks that we may need to perform when working with HTTP. For making requests and receiving responses, we employ the urllib.request module.

Retrieving the contents of a URL is a straightforward process when done using urllib. Load your Python interpreter and do the following:

>>> from urllib.request import urlopen
>>> response = urlopen('http://www.debian.org')
>>> response
<http.client.HTTPResponse object at 0x7fa3c53059b0>
>>> response.readline()
b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n'

We use the urllib.request.urlopen() function for sending a request and receiving a response for the resource at http://www.debian.org, in this case an HTML page. We...

You have been reading a chapter from
Learning Python Networking
Published in: Jun 2015
Publisher:
ISBN-13: 9781784396008
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime