A simple HTTP client implemented in Python

Overview

A simple HTTP client made after practicing Python

environment

Python 3.4.3

How to use

If the command is python3, please read as appropriate.

$python http1.0.py 'ip address' 'File name to save the returned html'

Supplement

Since it was troublesome to write the skipping of the header, I searched all the parts with two consecutive line breaks from the front and cut them off with slices. The exception is the style of catching everything for the time being.

Source code

http1.0.py


import socket
import sys
import traceback

def main():
    #Set max buffer size
    max_size = 8192

    argv = sys.argv
    argc = len(argv)

    #Check commandline parameter
    if(argc != 3):
        sys.stderr.write('Usage: python http1.0.py [ADDRESS] [FILE]\nor\n')
        sys.stderr.write('Usage: python3 http1.0.py [ADDRESS] [FILE]\n')
        exit()

    address = (argv[1], 80)
    filename = argv[2]

    try:
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    #Create socket
        client.connect(address)    #Connect
        client.sendall(b'GET / HTTP/1.0\r\n\r\n')    #Send GET command
        data = client.recv(max_size).decode('utf-8')    #Receive data and decode
        client.close()  #Close

        #Cut header
        index = data.find('\r\n\r\n')
        data = data[index:]

        #Write HTML file
        fout = open(filename, 'w')
        fout.write(data)
        fout.close()

    except:
        sys.stderr.write(traceback.format_exc())
        exit()


if __name__ == '__main__':
	main()

Recommended Posts

A simple HTTP client implemented in Python
Simple IRC client in python
Implementing a simple algorithm in Python 2
Run a simple algorithm in Python
Try drawing a simple animation in Python
Create a simple GUI app in Python
Write a simple greedy algorithm in Python
Write a simple Vim Plugin in Python 3
Simple gRPC in Python
Http request in python
Implemented SimRank in Python
Implemented Shiritori in Python
Set up a simple HTTPS server in Python 3
A simple Pub / Sub program note in Python
Create a simple momentum investment model in Python
Set up a simple SMTP server in Python
Take a screenshot in Python
Create a function in Python
Create a dictionary in Python
Write a super simple molecular dynamics program in python
Use HTTP cache in Python
A simple Python HTTP server that supports Range Requests
Make a simple Slackbot with interactive button in python
Make a bookmarklet in Python
Sudoku solver implemented in Python 3
Simple regression analysis in Python
Simple HTTP Server for python
Draw a heart in Python
6 Ball puzzle implemented in python
I implemented a Vim-like replacement command in Slackbot #Python
I made a simple typing game with tkinter in Python
How to specify a public directory Python simple HTTP server
A simple way to avoid multiple for loops in Python
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
First simple regression analysis in Python
Hit a command in Python (Windows)
Implemented image segmentation in python (Union-Find)
Simple OAuth 2 in Python (urllib + oauthlib)
Create a DI Container in Python
Write an HTTP / 2 server in Python
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Widrow-Hoff learning rules implemented in Python
Create a binary file in Python
Implemented label propagation method in Python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Implemented Perceptron learning rules in Python
Create a Kubernetes Operator in Python
Solve ABC037 A ~ C in Python
Draw a CNN diagram in Python
Create a random string in Python
Simple gacha logic written in Python
Implemented in 1 minute! LINE Notify in Python
Schedule a Zoom meeting in Python