Maier Ludentrop
Maier Ludentrop

Reputation: 31

Python sockets and requests

I'm a beginner in python and I'd like to know what the difference between socket and requests modules in python is. I'm sorry if this question format is bad, if this may be due to the fact that I do not know exactly how different protocols of Internet work, then I will be grateful if you share the relevant literature, so that I myself look for the answer to my question. Can you give examples?

Upvotes: 3

Views: 2329

Answers (1)

Keegan Murphy
Keegan Murphy

Reputation: 832

How Everything is Related

Requests is one of the most used, highest level python HTTP clients. It is built on urllib3, which is built on httpclient lib. These are all HTTP application protocol libraries that utilize sockets to make calls, and thus sockets is foundation of them all.

More on the Foundational Sockets

Sockets are widely used with most Operating Systems to communicate with networks, since they can control tons of types of connections and transmit data. HTTP libraries rely on sockets, and thus there is no HTTP/TCP protocol connections without them. However, on the opposite side, sockets can exist without HTTP.

Conclusion

In the end, the requests library is fundamentally built on several libs with sockets being the (basically) bottom level. You’ll find that requests is the easiest to use since it is the highest level with the most documentation. You can also use pysocks, however it is much more difficult since it is lower level, but there is some documentation out there that can lead you to the right direction for making your own connections and sending your own encoded data to act as an HTTP request. The main trade off is pysocks are harder to work with, but much more customizable and fast. Most python web-apps/scrapers use requests, and sockets are used more rarely by experts in some cases.

More Reading

Intro to HTTP: https://www.tutorialspoint.com/http/index.htm

Distinguishing Between HTTP and HTTPS protocols are very important to the web: https://www.cloudflare.com/learning/ssl/why-is-http-not-secure/

Requests (pythons objectively most widely used HTTP library): https://docs.python-requests.org/en/latest/

Pysocks (pythons use of sockets): https://pypi.org/project/PySocks/

The Relation of Senders/Receivers (advanced): https://www.geeksforgeeks.org/layers-of-osi-model/

Upvotes: 7

Related Questions