Posts

Introduction to network programming with Python #0003 HTTP 2020 Tutorial)

Image
  Table of Contents 1. Introduction to HTTP 2. Write a TCP server that serves as a HTTP server 3. A HTTP server demo 4. A HTTP server with multiprocessing 5. A HTTP server with multithreading 6. A HTTP server with gevent 7. A multitasking TCP server without threadings, multiprocessing, and gevent 8. HTTP Persistent connection Vs multiple connections 9. Improve our server with epoll (Linux only)

Introduction to regular expressions in Python (2020 tutorial)

Image
  1. Introduction to RegEx You have been surfing on the web for a long time and sometimes you need to sign up with your email address for some services from a website. Every website will validate your email address first and if you have entered an invalid one, you will be rejected to register. Here is the question, how come the web service validate the email address you just entered? The answer is regular expression. The web service will use a regular expression pattern to match against your email address; if match, you are allowed to register otherwise you will be rejected. if "foo@google.com" match "pattern":     allow_register() else:     deny_register( ) Actually regular expression is a tiny programming language embedded with python. You could use this tiny programming language through its re module. So let's dive in. 2. A simple match >>> import re >>> re.match(r'hello', 'hello world!') <re.Match object; span=(0, 5), mat

Introduction to multitasking with Python #003 gevent (2020 tutorial)

Image
  1 . Review of Iterator In python, we could achieve multitasking through another way by using gevent module. Howerver before introducing gevent, we have to review the concept of iterator. You may have used python for loop millions of times: for i in Iterable:     print(i) We have learned several iterable objects, str, list, tuple, dict and set. Those iterable objects or sequences could be used with the for loop. And we take that for granted. But look at the following example: >>> for i in 100: print(i) Traceback (most recent call last):   File "<pyshell#2>", line 1, in <module>     for i in 100: TypeError: 'int' object is not iterable >>>   Why cannot int type be used with for loop in Python and how could you make an object iterable and be used with for loop? Now, let's make an iterable object that could be used with for loop. We could use isinstance() built-in method to check whether an object is iterable or not. >>> fr