Answers for "how to get http request in python"

8

get request python

import requests

x = requests.get('https://w3schools.com')
print(x.status_code)
Posted by: Guest on August-14-2020
3

http server in python

# this program serves files from the server, if not found then returns 404


# Import socket module
from socket import *    

# Create a TCP server socket
#(AF_INET is used for IPv4 protocols)
#(SOCK_STREAM is used for TCP)

serverSocket = socket(AF_INET, SOCK_STREAM)

# Assign a port number
serverPort = 6789

# Bind the socket to server address and server port
serverSocket.bind(("", serverPort))

# Listen to at most 1 connection at a time
serverSocket.listen(1)

# Server should be up and running and listening to the incoming connections
while True:
	print('Ready to serve...')
	
	# Set up a new connection from the client
	connectionSocket, addr = serverSocket.accept()
	
	# If an exception occurs during the execution of try clause
	# the rest of the clause is skipped
	# If the exception type matches the word after except
	# the except clause is executed
	try:
		# Receives the request message from the client
		message =  connectionSocket.recv(1024)
		# Extract the path of the requested object from the message
		# The path is the second part of HTTP header, identified by [1]
		filename = message.split()[1]
		# Because the extracted path of the HTTP request includes 
		# a character '', we read the path from the second character 
		f = open(filename[1:])
		# Store the entire contenet of the requested file in a temporary buffer
		outputdata = f.read()
		# Send the HTTP response header line to the connection socket
		connectionSocket.send(str.encode("HTTP/1.1 200 OKrnrn"))
 
		# Send the content of the requested file to the connection socket
		for i in range(0, len(outputdata)):  
			connectionSocket.send(str.encode(outputdata[i]))
		connectionSocket.send(str.encode("rn"))
		
		# Close the client connection socket
		connectionSocket.close()

	except IOError:
		# Send HTTP response message for file not found
		connectionSocket.send(str.encode("HTTP/1.1 404 Not Foundrnrn"))
		connectionSocket.send(str.encode("<html><head></head><body><h1>404 Not Found</h1></body></html>rn"))
		# Close the client connection socket
		connectionSocket.close()

serverSocket.close()
Posted by: Guest on October-06-2020
0

how to access http page in pythion

#for Python 3.2
import urllib.request
page = urllib.request.urlopen("http://www.google.com")
print (page.read())
Posted by: Guest on October-04-2020
1

send get request python

import socket

target_host = "www.google.com" 
target_port = 80

# create a socket object 
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
 
# connect the client 
client.connect((target_host,target_port))  

# receive some data 
response = client.recv(4096)
print(f'Source Code: {response}')
http_response = repr(response)
Posted by: Guest on May-27-2020

Code answers related to "how to get http request in python"

Python Answers by Framework

Browse Popular Code Answers by Language