当前位置: 网站首页>小程序开发>小程序制作

汝州微信公众号开发【汝州网络推广】汝州建站、汝州网站维护、汝州网页制作、汝州微信小程序代运营公司

发表日期: 2021-04-28 09:50:59 浏览次数:118

汝州微信公众号开发【汝州网络推广】汝州建站、汝州网站维护、汝州网页制作、汝州微信小程序代运营公司


汝州市位于河南省中西部,因北汝河贯穿全境而得名,总面积1573平方公里,总人口120万,辖21个乡镇街道,459个行政村,是平顶山市下辖的县级市,也是河南省十个省直管县试点之一。

汝州是历代郡州治所。从公元606年的隋朝设立汝州以来,距今已有1400多年的历史。

汝州距离省会郑州90公里、洛阳70公里、平顶山60公里,焦枝(柳)铁路、宁洛高速、二广高速、林桐高速穿境而过,在1小时交通圈内通达郑州国际机场、洛阳机场,已建成和规划建设的郑万高铁、郑洛城际铁路、洛平漯周高铁、三洋铁路紧临汝州或在汝州设站。

汝州境内旅游景点达到852个,省级以上重点文物保护单位20个,其中国家重点文物保护单位10处,拥有风穴寺、九峰山等2个4A级景区,怪坡、中国汝瓷小镇、丹阳湖景区、汝水湾景区、汝河沙滩公园等5个3A级景区。共有37个具有开发价值的古寨古堡,夏店镇山顶村、蟒川镇半扎村、大峪镇青山后村、焦村镇张村等4个是国家级传统村落。九峰山、紫云山、蒋姑山、大红寨等风景名胜,罗圈地质公园冰川遗址是世界四大冰川地质遗址之一。

汝州市为国家园林城市、国家卫生城市、全国绿化模范市、全国文明城市城市,被评为全国第二批节水型社会建设达标市、全省水生态文明城市、全省首批全域旅游示范市、首批省级森林城市,荣获中国十佳绿色城市、全国文旅融合特色创新示范市、中国汝瓷之都等称号,海绵城市项目荣获中国人居环境范例奖,PPP项目实施和土地节约集约利用两项工作受到国务院通报表彰。 [1] 


Python3 常用代码整理

前言

    该文章主要作用是整理收集常用代码,用来在以后快速编写程序提供源码。

一;Python3 网络编程

    python 使用模块socket来完成TCP和UDP服务器和客户端的创建,跟进一步说明就是socket模块当中封装了代码用来调用TCP,UDP协议,生成符合TCP,UDP等协议的数据包。

    1.1;TCP客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import socket
 
target_host = " "
target_port = 80
 
#建立一个socket对象,支持IPV4协议,且为TCP套接字
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 
#链接客户端
client.connect((target_host,target_port))
 
#发送数据库
client.send(('GET / HTTP/1.1  Host: baidu.com ').encode())
 
#接收数据
response = client.recv(4096)
 
print(response)

    1.2;UDP客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import socket
 
target_host = " "
target_port = 80
 
#建立一个socket对象,支持IPV4协议,且为TCP套接字
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 
#发送数据库
client.sendto(('aaaaaaa').encode(),(target_host,target_port))
 
#接收数据
data,addr = client.recvfrom(4096)
 
print(data)

    1.3;TCP服务器

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
32
33
import socket
import threading
 
bind_ip = "0.0.0.0"
bind_port = 9999
 
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 
server.bind((bind_ip,bind_port))
server.listen(5)
 
print("[*] Listening on %s:%d" %(bind_ip,bind_port))
 
#客户处理线程
def handle_client(client_socket):
    #打印出客户端发送得到的内容
    request = client_socket.recv(1024)
    print("[*] Received: %s" % request)
 
    #返回一个数据包
    client_socket.send("ACK!")
 
    #关闭套接字
    client_socket.close
 
while True:
    client,addr = server.accept()
 
    print("[*] Accepted connection from : %s:%d" % (addr[0],addr[1]))
 
    #挂起客户端线程,处理传入数据
    client_handler = threading.Thread(target=handle_client,args=(client,))
    client_handler.start()

    1.4;替代netcat

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import sys
import socket
import getopt
import threading
import subprocess
 
#定义一些全局变量
listen = False
command = False
upload = False
execute = ""
target = ""
upload_destination = ""
port = 0
 
def usage():
    print("BHP Net Tool")
    print("Usage: bhpnet.py -t target_host -p port")
    print("-l --listen  - listen on [host]:[port] for incoming connections")
    print("-e --execute=file_to_run  - execute the give file upon receiving a connection")
    print("-c --command   - initialize a command shell")
    print("-u --upload = destination - upon receiving connection upload a file and write to [destination]")
 
    print(" ")
    print("Examples: ")
    print("bhpnet.py -t 192.168.1.1 -p 555 -l -c")
    print("bhpnet.py -t 192.168.1.1 -p 555 -l -u = c:\target.exe")
    print("bhpnet.py -t 192.168.1.1 -p 555 -l -e = "cat /etc/passwd" ")
    print("echo '123' | ./bhpnet.py  -t 192.168.1.1 -p 555")
    sys.exit(0)
 
def main():
    global listen
    global port
    global execute
    global command
    global upload_destination
    global target
 
    if not len(sys.arge[1:]):
        usage()
 
    #读取命令行选项
    try:
        opts,args = getopt.getopt(sys.argv[1:],"hle:t:p:cu:",["help","listen","execute","target","port","command","upload"])
    except getopt.GetoptError as err:
        print(str(err))
        usage()
 
    for o,a in opts:
        if in ("-h","--help"):
            usage()
        elif in ("-l","--listen"):
            listen = True
        elif in ("-e""--execute"):
            execute = a
        elif in ("-c","--commandshell"):
            command = True
        elif in ("-u","--upload"):
            upload_destination = a
        elif in ("-t","--target"):
            target = a
        elif in ("-p","--post"):
            port = int(a)
        else:
            assert False,"Unhandled Option"
 
    #监听?输入发送数据?
    if not listen and len(target) and port > 0:
        #从命令行读取内存数据
        #这里将阻塞,所以不在标准输入发送数据时候发送CTRL-D
        buffer = sys.stdin.read()
 
        #发送数据
        client_sender(buffer)
 
        if listen:
            server_loop()
 
if __import__== "__main__":
    main()
    #发送数据
    def client_sender(buffer):
        client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        try:
            #链接到目标主机
            client.connect((target,port))
            if len(buffer):
                client.send(buffer)
            while True:
                #现在等待数据回传
                recv_len = 1
                response = ""
                while recv_len:
                    data = client.recv(4096)
                    recv_len = len(data)
                    response += data
                    if recv_len < 4096:
                        break
                print(response)
 
                #等待更多的输入
                buffer = raw_input("")
                buffer += " "
 
                #发送出去
                client.send(buffer)
 
        except:
            print("[*] Exception! Exiting.")
            #关闭链接
            client.close()
 
    #tcp服务器
    def server_loop():
        global target
 
        #如果没有定义目标,那么我们监听所有端口
        if not len(target):
            target = "0.0.0.0"
 
        server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        server.bind((target,port))
 
        server.listen(5)
 
        while True:
            client_socket,addr = server.accept()
 
            #分拆一个县城处理新的客户端
            client_thread = threading.Thread(target=client_handler,args=(client_socket,))
            client_thread.start()
 
    def run_command(command):
        #换行
        command = command.rstrip()
        #运行命令并将输出返回
        try:
            output = subprocess.check_output(command,stderr=subprocess.STDOUT,shell=True)
        except:
            output = "Failed to execute command. "
 
        #将输出发送
        return output
 
    #文件上传,命令执行,shell
    def client_handler(client_socket):
        global upload
        global execute
        global command
 
        #检测上传文件
        if len(upload_destination):
            #读取所有的字符并写下目标
            file_buffer = ""
 
            #持续读取数据知道没有符合的数据
            while True:
                data = client_socket.recv(1024)
 
                if not data:
                    break
                else:
                    file_buffer += data
 
        #将接收到的数据写出来
        try:
            file_descriptor = open(upload_destination,"wb")
            file_descriptor.write(file_buffer)
            file_descriptor.close()
 
            #确定写入完成
            client_socket.send("Successfully saved file to %s " % upload_destination)
        except:
            client_socket.send("Failed to save file to %s " % upload_destination)
 
 
 
        #检查命令执行
        if len(execute):
            #运行命令
            output = run_command(execute)
            client_socket.send(output)
 
            #如果需要一个命令行shell,那么我们进入另一个循环
            if command:
                while True:
                    #跳出一个窗口
                    client_socket.send("<BHP:#>")
 
                    #接受文件直到发现换行符
                    cmd_buffer = ""
                    while " " not in cmd_buffer:
                        cmd_buffer += client_socket.recv(1024)
 
                        #返还命令输出
                        response = run_command(cmd_buffer)
 
                        #返回响应数据
                        client_socket.send(response)

二;文件操作

    open()函数的操作模式,记录如下表:

操作模式注释
‘r’读取(默认)
‘w’写入(先截断之前的内容)
‘x’写入,如果文件已经存在会产生异常
‘a’追加,将内容写入到已有文件的末尾
‘b’二进制模式
‘t’文本模式
‘+’
更新(即可读可写)

    2.1;读取文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def main():
    = None
    try:
        = open('somefile.txt','r',encoding='utf-8')
        print(f.read())
    except FileNotFoundError:
        print('无法打开指定的文件!')
    except LookupError:
        print('指定了未知的编码!')
    except UnicodeDecodeError:
        print('读取文件时编码错误')
    finally:
        if f:
            f.close()
 
if __name__ == '__main__':
    main()

    2.2;逐行读取文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import time
 
def main():
    print("整个文件全部读取")
    #全部读取整个文件内容
    with open('somefile.txt',mode='r',encoding='utf-8') as f:
       print(f.read())
 
    print("for-in 循环逐行读取")
    #for-in 循环逐行读取
    with open('somefile.txt',mode='r') as f:
        for line in f:
            print(line,end='')
            time.sleep(0.5)
    print(" ")
    print("文件按行读取到列表当中")
 
    #读取文件按行读取到列表当中
    with open('somefile.txt') as f:
        lines = f.readline()
    print(lines)
 
if __name__ == '__main__':
    main()

    2.3;文件写入    

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
32
33
34
from math import sqrt
 
def is_prime(n):
    """判断素数的函数"""
    assert n > 0
    for factor in range(2,int(sqrt(n)) + 1):
        if % factor == 0:
            return False
    return True if n != 1 else False
 
def main():
    filenames = ('a.txt','b.txt','c.txt')
    fs_list=[]
    try:
        for filename in filenames:
            fs_list.append(open(filename,'w',encoding='utf-8'))
        for number in range(1,10000):
            if is_prime(number):
                if number < 100:
                    fs_list[0].write(str(number) + ' ')
                elif number < 1000:
                    fs_list[1].write(str(number) + ' ')
                else:
                    fs_list[2].write(str(number) + ' ')
    except IOError as ex:
        print(ex)
        print('写文件时发生错误')
    finally:
        for fs in fs_list:
            fs.close()
    print('操作完成!')
 
if __name__ == '__main__':
    main()

    2.4;二进制写入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def main():
    try:
        with open('1.jpg','rb') as fs1:
            data = fs1.read()
            print(type(data))
        with open('2.jpg','wb') as fs2:
            fs2.write(data)
    except FileNotFoundError as e:
        print('指定的文件无法打开。')
    except IOError as e:
        print('读写文件时出现错误。')
    print('程序执行结束')
 
 
if __name__ == '__main__':
    main()

    2.5;读写JSON文件

    json的数据类型和Python的数据类型的对比

PythonJSON
dictobject
list,tuplearray
strstring
int,float,int- &float-derived Enums

number

True/Falsetrue/false
Nonenull

    使用Json模块,将字典,列表以JSON格式保存在文件当中。

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
import json
 
 
def main():
    mydict = {
        'name''张三',
        'age'333,
        'qq'952348,
        'friends': ['王''芳'],
        'cars': [
            {'brand''BYD''max_speed'180},
            {'brand''Au2di''max_speed'280},
            {'brand''Ben3z''max_speed'320}
        ]
    }
    try:
        with open('data.json''w', encoding='utf-8') as fs:
            json.dump(mydict, fs)
    except IOError as e:
        print(e)
    print('保存数据完成!')
 
 
if __name__ == '__main__':
    main()

三;进程与线程

    多进程

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
from multiprocessing import Process
from os import getpid
from random import randint
from time import time,sleep
 
def download_task(filename):
    print('启动下载进程,进程号[%d]'% getpid())
    print('开始下载%s...'%filename)
    time_to_download = randint(5,10)
    sleep(time_to_download)
    print('%s下载完成!耗费了%d秒'%(filename,time_to_download))
 
def main():
    start = time()
    p1 = Process(target=download_task,args=('test.pdf'))  #创建进程p1
    p1.start()                                            #启动进程p1
    p2 = Process(target=download_task,args=('test2.pdf'))
    p2.start()
    p1.join()                                             #等待P1进程结束
    p2.join()
    end = time()
    print('总共耗费了%.2f秒'%(end - start))
 
if __name__ == '__main__':
    main()

    多线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from random import randint
from threading import Thread
from time import time,sleep
 
def download(filename):
    print('开始下载%s....'%filename)
    time_to_download = randint(5,10)
    sleep(time_to_download)
    print('%s下载完成!耗费了%d秒' %(filename,time_to_download))
 
def main():
    start = time()
    t1 = Thread(target=download,args=('test.pdf')) #创建一个线程t1
    t1.start()                                     #启动线程t1
    t2 = Thread(target=download,args=('test2.pdf'))
    t2.start()
    t1.join()                                      #等待线程t1结束
    t2.join()
    end = time()
    print('总共耗费了%.3f秒' % (end - start))
 
if __name__ == '__main__':
    main()

微信图片_20210425092605.jpg

汝州微信公众号开发汝州网络推广汝州建站、汝州网站维护、汝州网页制作、汝州微信小程序代运营公司

400-111-6878
服务热线
顶部

备案号: 苏ICP备11067224号

CopyRight © 2011 书生商友信息科技 All Right Reserved

24小时服务热线:400-111-6878   E-MAIL:1120768800@qq.com   QQ:1120768800

  网址: http://www.768800.com  网站建设上往建站

关键词: 网站建设| 域名邮箱| 服务器空间| 网站推广| 上往建站| 网站制作| 网站设计| 域名注册| 网络营销| 网站维护|

企业邮箱| 虚拟主机| 网络建站| 网站服务| 网页设计| 网店美工设计| 网站定制| 企业建站| 网站设计制作| 网页制作公司|

400电话办理| 书生商友软件| 葬花网| 调温纤维| 海洋馆运营维护| 北京保安公司| 殡仪馆服务| 殡葬服务| 苏州殡葬一条龙| 朝阳殡葬| 苏州殡葬服务|

预约专家

欢迎您免费咨询,请填写以下信息,我们收到后会尽快与您联系

  

服务热线:400-111-6878