Created
May 28, 2024 16:16
-
-
Save Nehadsys/bc412d5428a4943cb95aa94ae26eb7e8 to your computer and use it in GitHub Desktop.
Python simple socket get content requests for webservers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ## Initializes a request | |
mysock.connect(('data.pr4e.org',80)) # connects to the request | |
cmd = 'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\r\n\r\n'.encode() # Gets the metadata from the HTTP host and encodes it too. | |
mysock.send(cmd) # Finally sends it | |
while True: | |
data = mysock.recv(512) # recives 512 characters only | |
if (len(data) < 1): # if less than 1 just break it. | |
break | |
print(data.decode()) # decodes the data | |
mysock.close() # closes it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment