Gabriel Cruz (he/him)
Posted on August 10, 2020
This is how I survive in web development using mainly Python.
Getting a web page, posting data to an API or making connections to FTP servers to download files automatically are super common tasks when you're programming. And, as with most tasks, Python makes it super easy to do them.
HTTP Requests
For the HTTP stuff, all you want is in the requests
library. Wanna get the best website ever?
import requests
response = requests.get('https://gmcruz.me')
response.content # gives the HTML for the page
response.status_code # gives the HTTP status code (200, 404, etc.)
Boom
How about sending data in a POST request to an API?
import requests
endpoint = 'https://httpbin.org/post'
body = {'user':'gabriel', 'password':'iloveyou'}
response = requests.post(endpoint, data = body)
response.json() # converts the response object to JSON
Done
Sockets and connections
What are Sockets?
Sockets are pretty much like electricity sockets. There's one on the wall with holes in it (the server-site, or listening socket) that waits for other sockets to plug in it (the client-side socket).What are Ports?
TCP sockets are bind to TCP ports. A port is just an integer that addresses a program (or rather, a process). This allows a host to have multiple processes using TCP sockets to run at the same time. When a packet comes in, it reads the port and sends the incoming data to the appropriate program.
If you come from a C background, the Python functions for socket handling pretty much mimic the C ones, but with that extra sugar that we all love.
To create a listening TCP socket on localhost
port 9849
import socket
host = 'localhost' # or any other host
port = 9849 # or any other port
my_socket = socket.socket() # create a new socket object
my_socket.bind((host, port)) # bind the socket to host and port
my_socket.listen() # start listening
conn, addr = my_socket.accept() # accept an incoming connection
To connect to the listening TCP socket at localhost
port 9849
import socket
host = 'localhost' # or any other host
port = 9849 # or any other port
my_socket = socket.socket()
my_socket.connect((host, port))
To send data through the connected socket
conn.send('hey there'.encode()) # on the server (listening) side
Or
my_socket.send('hey there'.encode()) # on the client side
Sockets speak in a different encoding than utf-8
(which Python uses). Don't forget to always .encode()
your data from utf-8
to bytes
before sending.
Finally, to receive data from a socket
max_bytes_to_recv = 1024
data = my_socket.recv(max_bytes_to_recv)
print(data.decode())
Similarly to encoding, don't forget to .decode()
your data from bytes
to utf-8
after receiving.
Posted on August 10, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.