Python 提供了两个基本的 socket 模块:
Socket它提供了标准的BSD Socket API。SocketServer它提供了服务器重心,可以简化网络服务器的开发。
下面讲解下 Socket模块功能。
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| from __future__ import print_function | |
| import argparse | |
| import os | |
| import linecache | |
| import sys | |
| #!/usr/bin/env python | |
| # A simple demonstration of using cairo to shape windows. | |
| # Natan 'whatah' Zohar | |
| import gtk | |
| import math | |
| class ShapedGUI: | |
| def __init__(self): | |
| self.window = gtk.Window() | |
| self.window.show() # We show here so the window gets a border on it by the WM |
| # -*- coding: utf-8 -*- | |
| import hashlib | |
| def file_md5sum(file_path): | |
| '''计算文件md5值''' | |
| hash_md5 = hashlib.md5() | |
| chunk_size = 4096 | |
| with open(file_path, "rb") as f: | |
| chunk = f.read(chunk_size) | |
| while bool(chunk): |
| # -*- coding: utf-8 -*- | |
| import os | |
| def print_size(size): | |
| '''打印文件大小''' | |
| print('Size: {0} Byte'.format(size)) | |
| print('Size: {0} KB'.format(size / 1024)) | |
| print('Size: {0} MB'.format(size / 1024 / 1024)) | |
| print('Size: {0} GB'.format(size / 1024 / 1024 / 1024)) |
| using System; // Console | |
| using System.Runtime.InteropServices; // DllImport、StructLayout | |
| using System.Windows.Forms; // Screen、Form | |
| using System.Drawing; // Rectangle | |
| using System.ComponentModel; | |
| using System.Diagnostics; // Process | |
| /** | |
| * https://docs.microsoft.com/en-us/dotnet/framework/app-domains/how-to-build-a-single-file-assembly | |
| * https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe |
| import turtle | |
| import math | |
| def lerp(a, b, t): | |
| """Linear interpolation function. returns a when t==0, returns b when t==1 | |
| and linearly interpolates for values inbetween""" | |
| return (a * (1 - t)) + (b * t) | |
| import logging | |
| try: | |
| from urllib.parse import ( | |
| urlencode, | |
| parse_qs, | |
| urlsplit, | |
| urlunsplit | |
| ) | |
| except ImportError: | |
| from urllib import urlencode |
| # -*- coding: utf-8 -*- | |
| import os | |
| import sys | |
| import multiprocessing as mp | |
| from multiprocessing import Pool | |
| from multiprocessing.dummy import Pool as ThreadPool | |
| from datetime import datetime | |
| try: | |
| import requests | |
| except ImportError: |
| # -*- coding: utf-8 -*- | |
| # need to upgrade the default Python version on Ubuntu 16.04 | |
| # refer: | |
| # https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get | |
| # https://aiohttp.readthedocs.io/en/stable/web_advanced.html#static-file-handling | |
| import os | |
| import logging | |
| try: | |
| from aiohttp import web |