Skip to content

Commit 06ee99b

Browse files
committed
socket
1 parent 3d70486 commit 06ee99b

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"python.pythonPath": "/usr/local/bin/python3",
2+
"python.pythonPath": "/usr/local/anaconda3/bin/python",
33
"python.testing.unittestArgs": [
44
"-v",
55
"-s",

PILtest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from PIL import Image, ImageDraw, ImageFont, ImageFilter
2+
3+
import random
4+
5+
# 随机字母:
6+
def rndChar():
7+
return chr(random.randint(65, 90))
8+
9+
# 随机颜色1:
10+
def rndColor():
11+
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
12+
13+
# 随机颜色2:
14+
def rndColor2():
15+
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
16+
17+
# 240 x 60:
18+
width = 60 * 4
19+
height = 60
20+
image = Image.new('RGB', (width, height), (255, 255, 255))
21+
# 创建Font对象:
22+
font = ImageFont.truetype('Arial.ttf', 36)
23+
# 创建Draw对象:
24+
draw = ImageDraw.Draw(image)
25+
# 填充每个像素:
26+
for x in range(width):
27+
for y in range(height):
28+
draw.point((x, y), fill=rndColor())
29+
# 输出文字:
30+
for t in range(4):
31+
draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
32+
# 模糊:
33+
image = image.filter(ImageFilter.BLUR)
34+
image.save('code.jpg', 'jpeg')

code.jpg

4.48 KB
Loading

socketclienttest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import socket
2+
import threading
3+
import time
4+
5+
6+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7+
# 建立连接:
8+
s.connect(('127.0.0.1', 19999))
9+
# 接收欢迎消息:
10+
print(s.recv(1024).decode('utf-8'))
11+
for data in [b'Michael', b'Tracy', b'Sarah']:
12+
# 发送数据:
13+
s.send(data)
14+
print('recv:',s.recv(1024).decode('utf-8'))
15+
q=input('请输入:')
16+
print(q,'sended')
17+
while q and q!='exit':
18+
s.send(q.encode('utf-8'))
19+
print('recv:' , s.recv(1024).decode('utf-8'))
20+
q=input('请输入:')
21+
print(q,'sended')
22+
s.send(b'exit')
23+
s.close()

sockettestsever.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import socket
2+
import threading
3+
import time
4+
5+
def tcplink(sock, addr):
6+
print('Accept new connection from %s:%s...' % addr)
7+
sock.send(b'Welcome!')
8+
while True:
9+
data = sock.recv(1024)
10+
print(data.decode('utf-8'),'from:',addr)
11+
time.sleep(1)
12+
if not data or data.decode('utf-8') == 'exit':
13+
break
14+
sock.send(('Hello, %s!' % data.decode('utf-8')).encode('utf-8'))
15+
sock.close()
16+
print('Connection from %s:%s closed.' % addr)
17+
18+
if __name__ == "__main__":
19+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
20+
s.bind(('127.0.0.1', 19999))
21+
s.listen(3)
22+
print('Waiting for connection...')
23+
24+
while True:
25+
# 接受一个新连接:
26+
sock, addr = s.accept()
27+
# 创建新线程来处理TCP连接:
28+
t = threading.Thread(target=tcplink, args=(sock, addr))
29+
t.start()

0 commit comments

Comments
 (0)