This forum is now read-only. Please use our new forums at discuss.codecademy.com.

How to use APIs with Python Forum

View Course

519 points
51d0874d631fe98ee000aabf_404662760
Submitted by
ET
almost 3 years ago

Question 17 Inquiry about .close()

Why is it that we don't have to invoke .close() on our opened URL, like we normally should with files that we open through command prompt or something.

We can just print and go along with our lives?


0 votes

permalink

To put it simply, there is no "opened" url.

What's actually happening is that every time you make a request, information is sent to that url, and then information is send back. It doesn't constantly stay "open", instead it "opens" when information needs to be sent or received.

However, in python, there is a way to "bind" what is called a socket(basically the same thing as a XMLHttpRequest() in JS) to a connection, as the following code demonstrates:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 8000))
s.listen(1)

The socket, s, will remain binded to the connection until it is aborted, or closed.

848 points
252f630586b97efb5f56a6b9ea92d511?s=140&d=retro
Submitted by
DarkxCoder
about 2 years ago