Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the writing techniques of Python

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the Python writing techniques". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the Python writing techniques"?

Python has become the industry standard in vulnerability development, and readers will find that most proof-of-concept tools are written in Python (except for security vulnerability detection tools written in Ruby). Python allows developers to write scripts for remote services, binaries, and C language libraries (or Java's Jython/). IronPython of Net) interacts in a quick and easy way. Its huge standard library of the "built-in battery" principle saves developers from relying on other frameworks or languages.

1. Environment configuration

For most of the projects or scripts you want to write, readers are advised to put all their dependencies in the same place (except for some used in special projects). To meet the above requirements, you need to use a tool called virtualenv (which is already included in Python3.3). This tool has a concise function to generate a separate environment for your Python project without disrupting the global environment:

# the way to generate a new environment is as follows: $virtualenv # or in an environment above Python3.3: $python3-mvenv # you need to activate this environment before you use it: $source / bin/activate# is also easy to disable the environment: $deactivate

two。 Install dependency packages

Many times you will find that personal tools written with the python libraries in the large python community can help us get results quickly. You can install these libraries through your personal software management pack or the available python package manager, the most authoritative of which is the pip tool. With pip, you can install these dependent packages globally (# pip install) or per user (

Pip install). Readers can install pip packages manually using the personal distribution package manager or based on the libraries provided by Python3.4.

There is a basic python package called iPython, which I usually install when I'm not 100% sure how to solve the current task and want to try some experiments. IPython is a commonly used python command line, which is written based on Python and has the following characteristics:

Dynamic object introspection

Complete the local namespace through Tab

A continuous historical record

Session log

Path completion

JIT Debugger

Automatic indent

As usual, installing via pip is easy: $pip install ipython

If you want to create tutorials or other text files, the notebook feature in ipython (now provided by jupyter) allows users to interact with the IPython command line through a personal browser, including tool support for markdown, mathjax, matplotlib, and so on.

(you can use them by installing jupyter (pip install jupyter) and turn on the notebook service through (jupyter notebook).

If you need to interact with HTTP services including JSON/XML during operation, I recommend a particularly useful requests dependent library. The python library can handle all kinds of operations that interact with web pages, such as encoding, decoding, parameters, markup, redirection and so on. For example, the code to request and parse a JSON resource is as follows:

R = requests.get ('https://api.github.com/user', auth= (' user', 'pass')) r.json () {upright privatekeeper repositories: 77,...}

Most HTML parsing and interaction can be left to the BeautifulSoup library, which can handle HTML input on any current browser, including repairing damaged code.

3. Interact with the network

Most of our goals are available on the web, and the installed standard library already contains general and useful python libraries, which I'll give you a brief introduction here. The socket module is a BSD socket API-based thin wrapper that is available on all general-purpose operating systems.

So if you already have C language socket programming experience, you can easily translate your code into python code. There are many convenient functions, such as the create_connection function, which can create a TCP socket and establish a connection between the local machine and a given host or port. Another wrapper is the sendall method, where some data can be transmitted on the line only if all given data is emitted, or if an error occurs, while the sendall method can try to retransmit the data.

From _ _ future__ import unicode_literalsimport sockets = socket.create_connection (('www.ernw.de', 80)) s.sendall (b'GET / HTTP/1.1Host: www.ernw.de') print (s.recv (1024))

Adding TSL encryption links is also very simple:

From _ _ future__ import unicode_literalsimport socketimport ssls = socket.create_connection (('www.ernw.de', 443)) s = ssl.wrap_socket (s) s.sendall (b'GET / HTTP/1.1Host: www.ernw.de') print (s.recv (1024))

The above functions can also be implemented in connections that are already in use:

From _ _ future__ import unicode_literalsimport socketimport ssls = socket.create_connection (('smtp.example.com', 25)) s.sendall (b'HELO smtp.example.comSTARTTLS') print (s.recv (1024)) s = ssl.wrap_socket (s) s.sendall (b'MAIL FROM:') print (s.recv (1024))

How you don't need these low-level service interactions, there are some modules that can provide high-level service interactions:

Smtplib

Ftplib

Poplib

Imaplib

Httplib (http client for Python 3 +)

Nntplib

Telnetlib (applied to service development and subsequent command-line sessions that require interaction)

Xmlrpclib (xmlrpc client for Python 3 +)

4. Binary operation or coding

When developing scripts that interact with services or files, you often find that you need to convert the data into different formats or encodings. In the Python2.x version, it is common to use encode or decode methods to convert strings between different formats.

"Hello World" .encode ("hex") "AAA=" .decode ("base64")

Unfortunately, this shortcut has been removed in the Python3.x version, and encode and decode methods currently only implement character encoding, such as utf-8, cp1250, iso8859, big5, and so on.

Instead, you can now implement hexadecimal coding using only two methods of the bytes type:

Bytes.fromhex ('414141') b'AAA'.hex () # start with Py3.5

For Base64 coding, you need to use another module (also available in the Python2.x version):

Import base64base64.b64encode (b'Hello World') import codecscodecs.encode (b'Hello World', 'base64') import binasciibinascii.b2a_base64 (b'Hello World')

URLs encoding or parsing can be implemented in the urllib.parse module (urllib in Python2.x version)

From urllib.parse import quote_plus, unquote_plusquote_plus ('Hello World+1=1337') # Hello+World%2B1%3D1337unquote_plus (' Hello+World') # Hello World

The general conversion between Python normal data types (such as int,float,str) and binary can be implemented in the stuct module:

Import structstruct.pack ('

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report