geekdoc-python-zh/docs/py4b/python-code-examples.md

172 lines
7.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Python 代码示例
> 原文:<https://www.pythonforbeginners.com/code-snippets-source-code/python-code-examples>
这个页面包含了我们到目前为止在网站上发布的所有 Python 脚本。
## **例题**
[使用 pywhois](/python-on-the-web/using-pywhois)
pywhois 是一个 Python 模块,用于检索域名的 whois 信息。pywhois 与 Python 2.4+一起工作,没有外部依赖[【源】](https://code.google.com/p/pywhois/ "pywhois")
[魔法 8 球](/code-snippets-source-code/magic-8-ball-written-in-python)
在这个脚本中,我使用了 8 个可能的答案,但请随意添加更多。魔法 8 球里面有 20 个答案,你可以在这里全部找到[。](https://en.wikipedia.org/wiki/Magic_8-Ball "magic8ball")
[使用 Python 的 CommandLineFu】](/code-snippets-source-code/commandlinefu-with-python)
当您想要使用基于 Web 的服务时,常见的第一步是查看它们是否有 API。幸运的是[Commandlinefu.com](https://www.commandlinefu.com/ "commandlinefudotcom")提供了一个,可以在这里找到
[用 os.walk 和 fnmatch 找到所有 mp3 文件](/code-snippets-source-code/os-walk-and-fnmatch-in-python)
在之前的一篇文章“Python 中的 OS.walk”中我描述了如何使用 os.walk并展示了一些如何在脚本中使用它的例子。在本文中我将展示如何使用 os.walk()模块函数遍历目录树,以及使用 fnmatch 模块匹配文件名。
[Python 中的端口扫描器](/code-snippets-source-code/port-scanner-in-python)
这篇文章将展示如何用 Python 编写一个小巧易用的端口扫描程序。
备份所有 MySQL 数据库,带时间戳。
使用 ConfigParser 备份所有 MySQL 数据库,每个文件一个,最后带有时间戳。
[谷歌命令行脚本](/code-snippets-source-code/google-command-line-script)
为了向 Web 搜索 API 发出请求,我们必须导入我们需要的模块。
[SSH 与 Python 的连接](/code-snippets-source-code/ssh-connection-with-python)
Pxssh 基于 pexpect。它的类扩展了 pexpect.spawn 来专门设置 SSH 连接。
[日期和时间脚本](/code-snippets-source-code/date-and-time-script)
这个脚本可以用来解析日期和时间。
[使用 Python 的小缩略器](/code-snippets-source-code/bitly-shortener-with-python)
Bitly 允许用户缩短、共享和跟踪链接(URL)。这是一种保存、分享和发现网络链接的方式。
[使用 Gmail 发送邮件](/code-snippets-source-code/using-python-to-send-email)
smtplib 模块定义了一个 SMTP 客户端会话对象,该对象可用于将
邮件发送到任何带有 SMTP 或 ESMTP 监听器守护进程的互联网机器。SMTP 代表简单邮件传输协议。
[查找以某个扩展名结尾的路径中的所有文件](https://www.pythonforbeginners.com/code-snippets-source-code/python-os-listdir-and-endswith)
这个简短的脚本使用 os.listdir 函数(属于 os 模块)来搜索给定的路径(".)用于所有以“”结尾的文件。txt”。
[命令行 speedtest.net 通过 tespeed](/code-snippets-source-code/command-line-speedtest-net-via-tespeed)
Speedtest.net 是一个连接分析网站,用户可以在这里对分布在世界各地的数百台服务器测试他们的网速。
[在电脑上搜索具体文件](/code-snippets-source-code/os-walk-and-fnmatch-in-python)
在本文中,我将展示如何使用 os.walk()模块函数遍历
目录树,以及使用 fnmatch 模块匹配文件名。
[用 Json 解析](https://www.pythonforbeginners.com/json/parse-json-objects-in-python)
当您想从各种 web 服务访问一个 API
并以 JSON 给出响应时,知道如何解析 JSON 对象是很有用的。
[获取 IP 地址的地理位置](/code-snippets-source-code/script-get-the-geo-location-of-an-ip-address)
又到了脚本的时间了,这个脚本将根据用户的输入地理定位一个 IP 地址。对于这个脚本,我们将使用一组 Python 模块来完成这个任务。
[从提示框中获取用户名](/code-snippets-source-code/script-get-the-username-from-a-prompt)
这个脚本将使用 raw_input 函数要求用户输入用户名。然后创建一个名为 user1 和 user2 的允许用户列表。控制语句检查来自用户的输入是否与允许用户列表中的输入相匹配。
[使用 Python 进行推文搜索](https://www.pythonforbeginners.com/code-snippets-source-code/tweet-search-with-python)
这个脚本将使用 Twitter API 来搜索推文。
[Python 中的日期和时间](/basics/python-datetime-timedelta)
在这篇文章中,我将向你展示如何不用它,只需使用
datetime.datetime.now()。
这篇文章的第二部分是关于 timedelta 类,在这个类中,我们可以看到两个日期、时间或日期时间实例之间微秒级分辨率的差异。
[Python 游戏:掷骰子](/code-snippets-source-code/game-rolling-the-dice)
这是一个经典的“掷骰子”程序。
[监控 Apache / Nginx 日志文件](/code-snippets-source-code/python-script-monitor-apachenginx-log-file)
这个小脚本将计算 Apache/Nginx 日志文件中的点击次数。
[Python 中的日志检查器](/code-snippets-source-code/python-script-log-checker)
该脚本将显示日志文件
变量中指定的文件中的所有条目。
[数数离生日还有几天](/basics/python-datetime-timedelta)
在这篇文章中,我将向您展示如何通过使用 datetime.datetime.now()打印出日期和时间。
[Python:猜谜游戏第二部](/code-snippets-source-code/python-guessing-game-part-2)
这个小程序扩展了我之前在这个
里写过的猜谜游戏:贴“猜谜游戏”。
[用 Python 写的猜谜游戏](/code-snippets-source-code/python-guessing-game/ "Guessing Game written in Python")
这个脚本是一个交互式猜谜游戏,它将要求用户猜一个 1 到 99 之间的
数字。
[Python 密码生成器](/code-snippets-source-code/script-password-generator)
可以使用[python 字符串](https://www.pythonforbeginners.com/basics/strings-formatting)和随机模块生成密码。
[OS Walk](https://www.pythonforbeginners.com/code-snippets-source-code/python-os-walk)
OS.walk()通过自顶向下或自底向上遍历目录树来生成目录树中的文件名。
[将公里/小时转换为英里/小时](/code-snippets-source-code/python-code-convert-kmh-to-mph)
这个脚本将速度从 KM/H 转换为 MPH这对于计算国外驾驶时的速度限制可能很方便特别是对于英国和美国的司机。
[获取网站的所有链接](/code-snippets-source-code/regular-expression-re-findall)
在这个脚本中,我们将使用 re 模块从任何网站获取所有链接。
[摄氏和华氏转换器](/code-snippets-source-code/python-code-celsius-and-fahrenheit-converter)
这个脚本将华氏温度转换为摄氏温度。
[如何在 Python 中使用 Vimeo API](/api/how-to-use-the-vimeo-api-in-python)
在这篇文章中,我们将学习如何在 Python 中使用 Vimeo API。
[显示 YouTube 上最受欢迎的视频](/api/using-the-youtube-api)
这个程序将展示我们如何使用 API 从 YouTube 中检索提要。
[计算平均分](/code-snippets-source-code/script-average-score)
这个脚本将计算三个值的平均值。
[检查您的外部 IP 地址](/code-snippets-source-code/check-your-external-ip-address)
这是一个简单的 Python 脚本,用于检查您拥有哪个外部 IP 地址。
[Python 刽子手游戏](/code-snippets-source-code/game-hangman)
这是一个经典游戏《刽子手》的 Python 脚本。
[Python 命令行 IMDB 刮刀](/code-snippets-source-code/imdb-crawler)
这个脚本将要求输入电影名称和年份,然后向 IMDB 查询。
## Python 代码示例
在这里,我们链接到提供 Python 代码示例的其他站点。
[ActiveState 代码——流行的 Python 食谱](https://code.activestate.com/recipes/langs/python/ "activestate_python_code")
snipplr . com
[NullegePython 源代码的搜索引擎](http://nullege.com/ "nullege")
[Snipt.net](https://snipt.net/public/tag/python/ "snipt.net")