超常用的Python代码片段 | 备忘单_from math import inf-程序员宅基地

技术标签: python  

本文作者BisratYalew共享了自己在项目中使用过的Python代码片段,希望能帮到其他开发人员,以减少时间并简化开发流程,该项目内容包含:String、Number、File、Functions、Classes、Date and Time、Regular Expressions、Web Data Handling、Asyncio以及Socket。

Python Strings
  • strip
  • lstrip
  • rstrip
  • upper
  • isupper
  • lower
  • islower
  • in
  • not in
  • ord
  • Template Strings
  • Pyperclip module
strip()
>>> check = '    Python is great!     '  # removes characters from both left and right based on the argument
>>> check.strip()
'Python is great'
lstrip()
>>> check.lstrip()
'Python is great!     ' ## removes characters from left based on the argument
rstrip()
>>> check.rstrip() ## removes characters from left based on the argument
'     Python is great!'
ord()
>>> ord('A'), ord('Z')
(65, 90)
>>> ord('0'), ord('9')
(48, 57)
>>> ord('a'), ord('z')
(97, 122)
>>> ord('B'), ord('Y')
(66, 89)

The in and not in Operators with string

>>> 'Python' in 'Python is great!'
True
>>> 'python' in 'Python'
False
>>> 'PYTHON' in 'Python is great!'
False
>>> '' in 'python'
True
>>> 'Python' not in 'python and javascript' ## Matches case
True

The in and not in Operators with list

>>> a = ['P', 'y', 't', 'h', 'o', 'n']
>>> 'p' in a
False
>>> 'P' in a
True
>>> 'o' in a
True

## On Integers List
>>> a = [1, 2, 3, 5]
>>> 5 in a
True
>>> 4 in a
False
>>> 1 in a
True
The upper(), lower(), isupper(), islower() string methods
>>> check = 'Hello world!'
>>> check = check.lower() ## converts check to lower
>>> check
'hello world!'
>>> check = check.isupper() ## checks whether check variable is upper or not. 
False
>>> check = check.islower() ## checks whether check variable is upper or not. 
True
>>> check = check.upper() ## converts check to upper
>>> check
'HELLO WORLD!'
>>> check = check.isupper() ## checks whether check variable is upper or not. 
True
>>> check = check.islower() ## checks whether check variable is upper or not. 
False
Template Strings

模板字符串是一种更简单的机制,用于处理用户生成的格式字符串

>>> from string import Template
>>> language = 'Python'
>>> t = Template('$name is great!')
>>> t.substitute(name=language) ## Substitutes name with language variable
'Python is great!'
#####Pyperclip
Pyperclip是一个Python库,可以帮助我们轻松复制和粘贴字符串
首先使用pip安装

pip install pyperclip

​```
>>> import pyperclip
>>> pyperclip.copy('Hello World') ## Copy Hello World
>>> pyperclip.paste()
'Hello World'
Number

Python中有三种数字类型

  • int (e.g. 2, 4, 20)
    bool (e.g. False and True, acting like 0 and 1)
  • float (e.g. 5.0, 1.6)
  • complex (e.g. 5+6j, 4-3j)
Basic Functions
a = pow(2, 3) ## Or: 2 ** 3
b = abs(5) ## <real> = abs(<num>)
Constants
from math import e, pi
Trigonometry
from math import cos, acos, sin, asin, tan, atan, degrees, radians
Logarithm
from math import log, log10, log2
<float> = log(<real> [, base])  # Base e, if not specified.
Infinity, nan
from math import inf, nan, isinf, isnan
Or:
float('inf'), float('nan')
Statistics
from statistics import mean, median, variance, pvariance, pstdev
Random
from random import random, randint, choice, shuffle
<float> = random()
<int>   = randint(from_inclusive, to_inclusive)
<el>    = choice(<list>)
shuffle(<list>)
Combinatorics

每个函数都返回一个迭代器。
如果要打印迭代器,需要将其传递给list()函数!

from itertools import product, combinations, combinations_with_replacement, permutations
>>> product([0, 1], repeat=3)
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), 
 (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]

>>> product('ab', '12')
[('a', '1'), ('a', '2'),
 ('b', '1'), ('b', '2')]

>>> combinations('abc', 2)
[('a', 'b'), ('a', 'c'), ('b', 'c')]

>>> combinations_with_replacement('abc', 2)
[('a', 'a'), ('a', 'b'), ('a', 'c'), 
 ('b', 'b'), ('b', 'c'), 
 ('c', 'c')]

>>> permutations('abc', 2)
[('a', 'b'), ('a', 'c'), 
 ('b', 'a'), ('b', 'c'), 
 ('c', 'a'), ('c', 'b')]
"""
def test_integer_numbers():
    """Integer type
    Int, or integer, is a whole number, positive or negative,
    without decimals, of unlimited length.
    """

    positive_integer = 1
    negative_integer = -3255522
    big_integer = 35656222554887711

    assert isinstance(positive_integer, int)
    assert isinstance(negative_integer, int)
    assert isinstance(big_integer, int)


def test_booleans():
    """Boolean
    Booleans represent the truth values False and True. The two objects representing the values
    False and True are the only Boolean objects. The Boolean type is a subtype of the integer type,
    and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the
    exception being that when converted to a string, the strings "False" or "True" are returned,
    respectively.
    """

    true_boolean = True
    false_boolean = False

    assert true_boolean
    assert not false_boolean

    assert isinstance(true_boolean, bool)
    assert isinstance(false_boolean, bool)

    # Let's try to cast boolean to string.
    assert str(true_boolean) == "True"
    assert str(false_boolean) == "False"


def test_float_numbers():
    """Float type
    Float, or "floating point number" is a number, positive or negative,
    containing one or more decimals.
    """

    float_number = 7.0
    # Another way of declaring float is using float() function.
    float_number_via_function = float(7)
    float_negative = -35.59

    assert float_number == float_number_via_function
    assert isinstance(float_number, float)
    assert isinstance(float_number_via_function, float)
    assert isinstance(float_negative, float)

    # Float can also be scientific numbers with an "e" to indicate
    # the power of 10.
    float_with_small_e = 35e3
    float_with_big_e = 12E4

    assert float_with_small_e == 35000
    assert float_with_big_e == 120000
    assert isinstance(12E4, float)
    assert isinstance(-87.7e100, float)


def test_complex_numbers():
    """Complex Type"""

    complex_number_1 = 5 + 6j
    complex_number_2 = 3 - 2j

    assert isinstance(complex_number_1, complex)
    assert isinstance(complex_number_2, complex)
    assert complex_number_1 * complex_number_2 == 27 + 8j


def test_number_operators():
    """Basic operations"""

    # Addition.
    assert 2 + 4 == 6

    # Multiplication.
    assert 2 * 4 == 8

    # Division always returns a floating point number.
    assert 12 / 3 == 4.0
    assert 12 / 5 == 2.4
    assert 17 / 3 == 5.666666666666667

    # Modulo operator returns the remainder of the division.
    assert 12 % 3 == 0
    assert 13 % 3 == 1

    # Floor division discards the fractional part.
    assert 17 // 3 == 5

    # Raising the number to specific power.
    assert 5 ** 2 == 25  # 5 squared
    assert 2 ** 7 == 128  # 2 to the power of 7

    # There is full support for floating point; operators with
    # mixed type operands convert the integer operand to floating point.
    assert 4 * 3.75 - 1 == 14.0

File

  • Read File
  • Write a File
  • Copy File
  • Move File
  • Readline
Read a file

在Python 3中,如果文件未以二进制模式打开,则编码将由locale.getpreferredencoding(False)或用户输入确定。

>>> with open("/etc/hosts", encoding="utf-8") as f:
...     content = f.read()
...
>>> print(type(content))
<class 'str'>

In python3 binary mode

>>> with open("/etc/hosts", "rb") as f:
...     content = f.read()
...
>>> print(type(content))
<class 'bytes'>

In python2

## The content of the file is a byte string, not a Unicode string.
>>> with open("/etc/passwd") as f:
...    content = f.read()
>>> print(type(content))
<type 'str'>
>>> print(type(content.decode("utf-8")))
<type 'unicode'>
Write a File
>>> file_content = "Python is great!"
>>> with open("check.txt", "w") as file:
...     file.write(file_content)
Copy a file
>>> from distutils.file_util import copy_file
>>> copy_file("a", "b")
('b', 1)
Move a File
>>> from distutils.file_util import move_file
>>> move_file("./a", "./b")
'./b'
Readline
## If you are not using linux machine try to change the file path to read
>>> with open("/etc/hosts") as f:
...     for line in f:
...         print(line, end='')
...
127.0.0.1       localhost
255.255.255.255     broadcasthost
::1             localhost

Python Functions

  • Lambda
  • Declare Function
  • Document Function
  • Get Function Name
  • Arguments
  • Decorator
  • Generator
Lambda
>>> fn = lambda x: x**3
>>> fn(2)
8

>>> (lambda x: x**3)(2)
8

>>> (lambda x: [x * _ for _ in range(10)])(3)
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
Declare Function
def fn_name(): ## Where fn_name represents the function name
    ........
    ........
Get Function Name
## First Declare a function 
def fn_name():
    .......
    .......

>>> fn_name.__name__
'fn_name'
Document a Function
## Document a function with three single quotes
def check():
    '''This function is documented'''
    return

>>> check.__doc__
'This function is documented'
Arguments
>>> def multiply(a, b=0): ## b is 0 by default
...     return a * b
...
>>> multiply(1, 3) ## 3 * 1
3
>>> multiply(5) ## 5 * 0
0
>>> multiply(5, b=3) ## 5 * 3
15
Generator
def count(start, step):
    while True:
        yield start
        start += step

>>> counter = count(10, 5)
>>> next(counter)
(15)

>>> next(counter) ## Increments by 5 from the previous result
(20)

>>> next(counter)
(25)

>>> next(counter), next(counter), next(counter)
(30, 35, 40)
Decorator
>>> from functools import wraps

>>> def decorator_func(func):
...     @wraps(func)
...     def wrapper(*args, **kwargs):
...         print("Before calling {}.".format(func.__name__))
...         ret = func(*args, **kwargs)
...         print("After calling {}.".format(func.__name__))
...         return ret
...     return wrapper
...
>>> @decorator_func
... def check():
...     print("Inside check function.")
...
>>> check()
Before calling check.
Inside check function.
After calling check.

Classes

  • Definition
  • Class Objects
  • Instance Objects
  • Method Objects
  • Inheritance
  • Multiple Inheritance

Class Definition

Python是一种面向对象的编程语言。 Python中的几乎所有东西都是一个对象,具有其属性和方法。 Class类似于对象构造函数,或者是用于创建对象的“蓝图”。

def test_class():
    """Class definition."""

    # Class definitions, like function definitions (def statements) must be executed before they
    # have any effect. (You could conceivably place a class definition in a branch of an if
    # statement, or inside a function.)

    class GreetingClass:
        """Example of the class definition
        This class contains two public methods and doesn't contain constructor.
        """
        name = 'Bisrat'

        def say_hello(self):
            """Class method."""
            # The self parameter is a reference to the class itself, and is used to access variables
            # that belongs to the class. It does not have to be named self , you can call it
            # whatever you like, but it has to be the first parameter of any function in the class.
            return 'Hello ' + self.name

        def say_goodbye(self):
            """Class method."""
            return 'Goodbye ' + self.name

    # When a class definition is entered, a new namespace is created, and used as the local scope —
    # thus, all assignments to local variables go into this new namespace. In particular, function
    # definitions bind the name of the new function here.

    # Class instantiation uses function notation. Just pretend that the class object is a
    # parameterless function that returns a new instance of the class. For example the following
    # code will creates a new instance of the class and assigns this object to the local variable.
    greeter = GreetingClass()

    assert greeter.say_hello() == 'Hello Bisrat'
    assert greeter.say_goodbye() == 'Goodbye Bisrat'

>>> print(type(content))
<class 'str'>

Class Objects

def test_class_objects():
    """Class Objects.
    Class objects support two kinds of operations:
    - attribute references
    - instantiation.
    """

    # ATTRIBUTE REFERENCES use the standard syntax used for all attribute references in
    # Python: obj.name. Valid attribute names are all the names that were in the class’s namespace
    # when the class object was created. For class MyCounter the following references are valid
    # attribute references:

    class ComplexNumber:
        """Example of the complex numbers class"""

        real = 0
        imaginary = 0

        def get_real(self):
            """Return real part of complex number."""
            return self.real

        def get_imaginary(self):
            """Return imaginary part of complex number."""
            return self.imaginary

    assert ComplexNumber.real == 0

    # __doc__ is also a valid attribute, returning the docstring belonging to the class
    assert ComplexNumber.__doc__ == 'Example of the complex numbers class'

    # Class attributes can also be assigned to, so you can change the value of
    # ComplexNumber.counter by assignment.
    ComplexNumber.real = 10
    assert ComplexNumber.real == 10

    # CLASS INSTANTIATION uses function notation. Just pretend that the class object is a
    # parameterless function that returns a new instance of the class. For example
    # (assuming the above class):
    complex_number = ComplexNumber()

    assert complex_number.real == 10
    assert complex_number.get_real() == 10

    # Let's change counter default value back.
    ComplexNumber.real = 10
    assert ComplexNumber.real == 10

    # The instantiation operation (“calling” a class object) creates an empty object. Many classes
    # like to create objects with instances customized to a specific initial state. Therefore a
    # class may define a special method named __init__(), like this:

    class ComplexNumberWithConstructor:
        """Example of the class with constructor"""
        def __init__(self, real_part, imaginary_part):
            self.real = real_part
            self.imaginary = imaginary_part

        def get_real(self):
            """Return real part of complex number."""
            return self.real

        def get_imaginary(self):
            """Return imaginary part of complex number."""
            return self.imaginary

    complex_number = ComplexNumberWithConstructor(3.0, -4.5)
    assert complex_number.real, complex_number.imaginary == (3.0, -4.5)

Instance Object

def test_instance_objects():
    # DATA ATTRIBUTES need not be declared; like local variables, they spring into existence when
    # they are first assigned to. For example, if x is the instance of MyCounter created above,
    # the following piece of code will print the value 16, without leaving a trace.

    # pylint: disable=too-few-public-methods
    class DummyClass:
        pass

    dummy_instance = DummyClass()

    # pylint: disable=attribute-defined-outside-init
    dummy_instance.temporary_attribute = 1
    assert dummy_instance.temporary_attribute == 1
    del dummy_instance.temporary_attribute

Method Objects

class MyCounter:
    """A simple example of the counter class"""
    counter = 10

    def get_counter(self):
        """Return the counter"""
        return self.counter

    def increment_counter(self):
        """Increment the counter"""
        self.counter += 1
        return self.counter


def test_method_objects():
    """Method Objects."""


    # object types can have methods as well. For example, list objects have methods called append,

    counter = MyCounter()
    assert counter.get_counter() == 10

    get_counter = counter.get_counter
    assert get_counter() == 10



    assert counter.get_counter() == 10
    assert MyCounter.get_counter(counter) == 10

Inheritance

# pylint: disable=too-few-public-methods
class Person:
    """Example of the base class"""
    def __init__(self, name):
        self.name = name

    def get_name(self):
        """Get person name"""
        return self.name


# The syntax for a derived class definition looks like this.
class Employee(Person):

    def __init__(self, name, staff_id):
        Person.__init__(self, name)
        # You may also use super() here in order to avoid explicit using of parent class name:
        # >>> super().__init__(name)
        self.staff_id = staff_id

    def get_full_id(self):
        """Get full employee id"""
        return self.get_name() + ', ' + self.staff_id


def test_inheritance():
    """Inheritance."""

    # There’s nothing special about instantiation of derived classes: DerivedClassName() creates a
    # new instance of the class. Method references are resolved as follows: the corresponding class
    # attribute is searched, descending down the chain of base classes if necessary, and the method
    # reference is valid if this yields a function object.
    person = Person('Bisrat')
    employee = Employee('John', 'A23')

    assert person.get_name() == 'Bisrat'
    assert employee.get_name() == 'John'
    assert employee.get_full_id() == 'John, A23'

    # Python has two built-in functions that work with inheritance:
    #
    # - Use isinstance() to check an instance’s type: isinstance(obj, int) will be True only if
    # obj.__class__ is int or some class derived from int.
    #
    # - Use issubclass() to check class inheritance: issubclass(bool, int) is True since bool is
    # a subclass of int. However, issubclass(float, int) is False since float is not a subclass
    # of int.

    assert isinstance(employee, Employee)
    assert not isinstance(person, Employee)

    assert isinstance(person, Person)
    assert isinstance(employee, Person)

    assert issubclass(Employee, Person)
    assert not issubclass(Person, Employee)

Multiple Inheritance

def test_multiple_inheritance():
    """Multiple Inheritance"""
    class Clock:
        """Clock class"""

        time = '10:17 PM'

        def get_time(self):
            """Get current time
            Method is hardcoded just for multiple inheritance illustration.
            """
            return self.time


    class Calendar:
        """Calendar class"""

        date = '12/08/2018'

        def get_date(self):
            """Get current date
            Method is hardcoded just for multiple inheritance illustration.
            """
            return self.date

    # Python supports a form of multiple inheritance as well. A class definition with multiple
    # base classes looks like this.
    class CalendarClock(Clock, Calendar):

    calendar_clock = CalendarClock()

    assert calendar_clock.get_date() == '12/08/2018'
    assert calendar_clock.get_time() == '11:23 PM'

Date and Time

  • DateTime
  • Time Delta
  • Calendar
datetime
from datetime import date 
from datetime import time
from datetime import datetime

>>> today = date.today()
>>> print(today)
2019-03-04

>>> print(today.day, today.month, today.year)
(4, 3, 2019)

>>> print(today.weekday()) ## returns an integers in the range 0 to 6, where 0 represents Monday and 6 represents Sunday.
0

>>> today = datetime.now()
>>> print(today)
2019-03-04 21:55:56.228000

>>> time = datetime.time(datetime.now())
>>> print(time) 
21:56:16.040000
Time Delta

This module is used to perform date and time calculations.

from datetime import date 
from datetime import time
from datetime import datetime
from datetime import timedelta

print(timedelta(days=365, hours=5, minutes=1)) # 365 days, 5:01:00
today  = datetime.now()
print(today + timedelta(days=60)) # 2019-03-29 18:41:46.720811
print(today - timedelta(days=57)) # 2018-12-02 18:42:36.774421

timedelta对象采用以下参数:天,秒,微秒,毫秒,分钟,小时,周。 要查找过去或将来的日期,只需使用带有当前日期所需差异的加号或减号。

Calendar

日历相关操作并以格式化方式显示

import calendar

c = calendar.TextCalendar(calendar.MONDAY)
st = c.formatmonth(2017, 1, 0,0)
print(st)

hc = calendar.HTMLCalendar(calendar.MONDAY)
st = hc.formatmonth(2017, 1)
print(st)

#Output
'''
    January 2017
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

<table border="0" cellpadding="0" cellspacing="0" class="month">
<tr><th colspan="7" class="month">January 2017</th></tr>
<tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr>
<tr><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="sun">1</td></tr>
<tr><td class="mon">2</td><td class="tue">3</td><td class="wed">4</td><td class="thu">5</td><td class="fri">6</td><td class="sat">7</td><td class="sun">8</td></tr>
<tr><td class="mon">9</td><td class="tue">10</td><td class="wed">11</td><td class="thu">12</td><td class="fri">13</td><td class="sat">14</td><td class="sun">15</td></tr>
<tr><td class="mon">16</td><td class="tue">17</td><td class="wed">18</td><td class="thu">19</td><td class="fri">20</td><td class="sat">21</td><td class="sun">22</td></tr>
<tr><td class="mon">23</td><td class="tue">24</td><td class="wed">25</td><td class="thu">26</td><td class="fri">27</td><td class="sat">28</td><td class="sun">29</td></tr>
<tr><td class="mon">30</td><td class="tue">31</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td></tr>
</table>
'''

.HTMLCalendar以表格的形式返回HTML代码。Calendar.SUNDAY表示格式化日历中的第一天是星期日,在上边的例子中,使用了Calendar.MONDAY。因此,我们可以在输出中看到表示中的第一天是星期一。

Iterating through dates of a month
import calendar

c = calendar.TextCalendar(calendar.MONDAY)
for i in c.itermonthdays(2017, 8):
    print(i, end = ' ')
print('')
for name in calendar.month_name:
    print(name, end = ' ')
print('')
for day in calendar.day_name:
    print(day, end = ' ')
'''
Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 0 0 0
 January February March April May June July August September October November December
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
'''

Regular Expressions

非特殊字符匹配

img

Regular Expression Examples

img

Web Data Handling

  • Make a Request
  • Read
  • Get Code
  • HTML Parsing
  • JSON Parsing
  • XML Parsing
Make a Request
import urllib.request ## urllib module is used to send request and receive response from a server. It can used to get html / JSON / XML data from an api.

webData = request.urlopen("http://www.google.com") ## It opens a connection to google.com and returns an object of class http.client.HTTPResponse

Read
.read()返回网页的HTML数据。

Get Code
.getcode() 返回连接建立的状态代码。

HTML Parsing

from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def error(self, message):
        pass

parser = MyHTMLParser()
f = open("check.html")
if f.mode == 'r':  # file successfully opened
    contents = f.read()
    parser.feed(contents)

JSON Parsing

import json
json_data = json.loads(response)

XML Parsing

import xml.dom.minidom
doc = minidom.parse("check.xml")

Asyncio

  • Definition
  • asyncio.run
  • Socket with Asyncio
  • Move File
  • Readline

asyncio是Python 3.5中包含的一个库,采用非常非常明确的异步编程方法:只有标记为async的方法编写的代码才能以异步方式调用任何代码。

asyncio.run

Python 3.7中的新功能

>>> import asyncio
>>> from concurrent.futures import ThreadPoolExecutor

>>> e = ThreadPoolExecutor()
>>> async def read_file(file_):
...     loop = asyncio.get_event_loop()
...     with open(file_) as f:
...         return (await loop.run_in_executor(e, f.read))
...
>>> ret = asyncio.run(read_file('/etc/passwd'))
Socket with Asyncio
import asyncio
import socket

host = 'localhost'
port = 9527
loop = asyncio.get_event_loop()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setblocking(False)
s.bind((host, port))
s.listen(10)

async def handler(conn):
    while True:
        msg = await loop.sock_recv(conn, 1024)
        if not msg:
            break
        await loop.sock_sendall(conn, msg)
    conn.close()

async def server():
    while True:
        conn, addr = await loop.sock_accept(s)
        loop.create_task(handler(conn))

loop.create_task(server())
loop.run_forever()
loop.close()
output: (bash 1)

$ nc localhost 9527
Hello
Hello
output: (bash 2)

$ nc localhost 9527
World
World

Socket

获取主机名

文件传输协议(ftp)客户端

文件传输协议(ftp)服务器

Get Hostname
>>> import socket
>>> socket.gethostname()
'root@bisratyalew'

>>> hostname = socket.gethostname()

>>> socket.gethostbyname('localhost')
'127.0.0.1'

>>> socket.gethostbyname(hostname) ### Returns ip address
'192.168.1.3'
FTP Client
import socket                   # Import socket module

port = 60000                    # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
s.bind((host, port))            # Bind to the port
s.listen(5)                     # Now wait for client connection.

print('Server listening....')

while True:
    conn, addr = s.accept()     # Establish connection with client.
    print('Got connection from', addr)
    data = conn.recv(1024)
    print('Server received', repr(data))

    filename = 'mytext.txt'
    with open(filename, 'rb') as f:
        in_data = f.read(1024)
        while in_data:
            conn.send(in_data)
            print('Sent ', repr(in_data))
            in_data = f.read(1024)

    print('Done sending')
    conn.send('Thank you for connecting')
    conn.close()


# client side server

import socket                   # Import socket module

s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
port = 60000                    # Reserve a port for your service.

s.connect((host, port))
s.send("Hello server!")

with open('received_file', 'wb') as f:
    print('file opened')
    while True:
        print('receiving data...')
        data = s.recv(1024)
        print('data=%s', (data))
        if not data:
            break
        # write data to a file
        f.write(data)

f.close()
print('Successfully get the file')
s.close()
print('connection closed')
FTP Server
"""
    File transfer protocol used to send and receive files using FTP server.
    Use credentials to provide access to the FTP client 
"""

from ftplib import FTP
ftp = FTP('xxx.xxx.x.x')  # Enter the ip address or the domain name here
ftp.login(user='username', passwd='password')
ftp.cwd('/Enter the directory here/')

"""
    The file which will be received via the FTP server
    Enter the location of the file where the file is received
"""

def recieve_file():
    file_name = 'example.txt'   """ Enter the location of the file """
    with open(file_name, 'wb') as LocalFile:
        ftp.retrbinary('RETR ' + file_name, LocalFile.write, 1024)
    ftp.quit()

"""
    The file which will be sent via the FTP server
    The file send will be send to the current working directory
"""

def send_file():
    file_name = 'example.txt'   """ Enter the name of the file """
    with open(file_name, 'rb') as LocalFile:
        ftp.storbinary('STOR ' + file_name, LocalFile)
    ftp.quit()

项目地址:

https://github.com/BisratYalew/python-cheatsheet

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sinat_38682860/article/details/88743075

智能推荐

攻防世界_难度8_happy_puzzle_攻防世界困难模式攻略图文-程序员宅基地

文章浏览阅读645次。这个肯定是末尾的IDAT了,因为IDAT必须要满了才会开始一下个IDAT,这个明显就是末尾的IDAT了。,对应下面的create_head()代码。,对应下面的create_tail()代码。不要考虑爆破,我已经试了一下,太多情况了。题目来源:UNCTF。_攻防世界困难模式攻略图文

达梦数据库的导出(备份)、导入_达梦数据库导入导出-程序员宅基地

文章浏览阅读2.9k次,点赞3次,收藏10次。偶尔会用到,记录、分享。1. 数据库导出1.1 切换到dmdba用户su - dmdba1.2 进入达梦数据库安装路径的bin目录,执行导库操作  导出语句:./dexp cwy_init/[email protected]:5236 file=cwy_init.dmp log=cwy_init_exp.log 注释:   cwy_init/init_123..._达梦数据库导入导出

js引入kindeditor富文本编辑器的使用_kindeditor.js-程序员宅基地

文章浏览阅读1.9k次。1. 在官网上下载KindEditor文件,可以删掉不需要要到的jsp,asp,asp.net和php文件夹。接着把文件夹放到项目文件目录下。2. 修改html文件,在页面引入js文件:<script type="text/javascript" src="./kindeditor/kindeditor-all.js"></script><script type="text/javascript" src="./kindeditor/lang/zh-CN.js"_kindeditor.js

STM32学习过程记录11——基于STM32G431CBU6硬件SPI+DMA的高效WS2812B控制方法-程序员宅基地

文章浏览阅读2.3k次,点赞6次,收藏14次。SPI的详情简介不必赘述。假设我们通过SPI发送0xAA,我们的数据线就会变为10101010,通过修改不同的内容,即可修改SPI中0和1的持续时间。比如0xF0即为前半周期为高电平,后半周期为低电平的状态。在SPI的通信模式中,CPHA配置会影响该实验,下图展示了不同采样位置的SPI时序图[1]。CPOL = 0,CPHA = 1:CLK空闲状态 = 低电平,数据在下降沿采样,并在上升沿移出CPOL = 0,CPHA = 0:CLK空闲状态 = 低电平,数据在上升沿采样,并在下降沿移出。_stm32g431cbu6

计算机网络-数据链路层_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输-程序员宅基地

文章浏览阅读1.2k次,点赞2次,收藏8次。数据链路层习题自测问题1.数据链路(即逻辑链路)与链路(即物理链路)有何区别?“电路接通了”与”数据链路接通了”的区别何在?2.数据链路层中的链路控制包括哪些功能?试讨论数据链路层做成可靠的链路层有哪些优点和缺点。3.网络适配器的作用是什么?网络适配器工作在哪一层?4.数据链路层的三个基本问题(帧定界、透明传输和差错检测)为什么都必须加以解决?5.如果在数据链路层不进行帧定界,会发生什么问题?6.PPP协议的主要特点是什么?为什么PPP不使用帧的编号?PPP适用于什么情况?为什么PPP协议不_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输

软件测试工程师移民加拿大_无证移民,未受过软件工程师的教育(第1部分)-程序员宅基地

文章浏览阅读587次。软件测试工程师移民加拿大 无证移民,未受过软件工程师的教育(第1部分) (Undocumented Immigrant With No Education to Software Engineer(Part 1))Before I start, I want you to please bear with me on the way I write, I have very little gen...

随便推点

Thinkpad X250 secure boot failed 启动失败问题解决_安装完系统提示secureboot failure-程序员宅基地

文章浏览阅读304次。Thinkpad X250笔记本电脑,装的是FreeBSD,进入BIOS修改虚拟化配置(其后可能是误设置了安全开机),保存退出后系统无法启动,显示:secure boot failed ,把自己惊出一身冷汗,因为这台笔记本刚好还没开始做备份.....根据错误提示,到bios里面去找相关配置,在Security里面找到了Secure Boot选项,发现果然被设置为Enabled,将其修改为Disabled ,再开机,终于正常启动了。_安装完系统提示secureboot failure

C++如何做字符串分割(5种方法)_c++ 字符串分割-程序员宅基地

文章浏览阅读10w+次,点赞93次,收藏352次。1、用strtok函数进行字符串分割原型: char *strtok(char *str, const char *delim);功能:分解字符串为一组字符串。参数说明:str为要分解的字符串,delim为分隔符字符串。返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。其它:strtok函数线程不安全,可以使用strtok_r替代。示例://借助strtok实现split#include <string.h>#include <stdio.h&_c++ 字符串分割

2013第四届蓝桥杯 C/C++本科A组 真题答案解析_2013年第四届c a组蓝桥杯省赛真题解答-程序员宅基地

文章浏览阅读2.3k次。1 .高斯日记 大数学家高斯有个好习惯:无论如何都要记日记。他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?高斯出生于:1777年4月30日。在高斯发现的一个重要定理的日记_2013年第四届c a组蓝桥杯省赛真题解答

基于供需算法优化的核极限学习机(KELM)分类算法-程序员宅基地

文章浏览阅读851次,点赞17次,收藏22次。摘要:本文利用供需算法对核极限学习机(KELM)进行优化,并用于分类。

metasploitable2渗透测试_metasploitable2怎么进入-程序员宅基地

文章浏览阅读1.1k次。一、系统弱密码登录1、在kali上执行命令行telnet 192.168.26.1292、Login和password都输入msfadmin3、登录成功,进入系统4、测试如下:二、MySQL弱密码登录:1、在kali上执行mysql –h 192.168.26.129 –u root2、登录成功,进入MySQL系统3、测试效果:三、PostgreSQL弱密码登录1、在Kali上执行psql -h 192.168.26.129 –U post..._metasploitable2怎么进入

Python学习之路:从入门到精通的指南_python人工智能开发从入门到精通pdf-程序员宅基地

文章浏览阅读257次。本文将为初学者提供Python学习的详细指南,从Python的历史、基础语法和数据类型到面向对象编程、模块和库的使用。通过本文,您将能够掌握Python编程的核心概念,为今后的编程学习和实践打下坚实基础。_python人工智能开发从入门到精通pdf