FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vcs/_4.python/import_module/module_socket.py at master · bunshue/vcs · GitHub
bunshue
/
vcs
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
3
Code
Issues
0
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vcs
/
_4.python
/
import_module
/
module_socket.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
344 lines (251 loc) · 9.91 KB
Breadcrumbs
vcs
/
_4.python
/
import_module
/
module_socket.py
Copy path
File metadata and controls
344 lines (251 loc) · 9.91 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""
socket
"""
import
sys
import
socket
print
(
"------------------------------------------------------------"
)
# 60個
import
socket
hostname
=
socket
.
gethostname
()
print
(
"取得 hostname :"
,
hostname
)
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
"""
import socket
socket.socket(family, type, proto)
# family:IPv4 本機、IPv4 網路、IPv6 網路。
# type:使用 TCP 或 UDP 方式。
# protocol: 串接協定 ( 通常預設 0 )。
"""
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
import
socket
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
s
.
connect
((
"8.8.8.8"
,
80
))
ip
=
s
.
getsockname
()[
0
]
print
(
ip
)
s
.
close
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
import
socket
hostname
=
"google.com"
print
(
"Hostname :"
,
hostname
)
print
(
"Host :"
,
socket
.
gethostbyname
(
hostname
))
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
host
=
"127.0.0.1"
# 主機的IP
port
=
2255
# 連接port編號
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
# 建立socket物件
s
.
bind
((
host
,
port
))
# 綁定IP和port
s
.
listen
(
5
)
# TCP監聽
print
(
f"Server在
{
host
}
:
{
port
}
"
)
print
(
"waiting for connection ..."
)
while
True
:
conn
,
addr
=
s
.
accept
()
# 被動接收客戶連線
print
(
f"目前連線網址
{
addr
}
"
)
data
=
conn
.
recv
(
1024
)
# 接收客戶的數據
print
(
data
)
# 列印數據
conn
.
sendall
(
b"HTTP/1.1 200 OK
\r
\n
\r
\n
Welcome to Deepmind"
)
conn
.
close
()
# 關閉連線
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
import
socket
host
=
"127.0.0.1"
# 主機的IP
port
=
2255
# 連接port編號
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
# 建立socket物件
s
.
connect
((
host
,
port
))
data
=
input
(
"請輸入資料 : "
)
s
.
send
(
data
.
encode
())
# 轉成 bytes 資料傳送
receive_data
=
s
.
recv
(
1024
).
decode
()
# 接收所傳來的資料同時解成字串
print
(
f"接收數據
{
receive_data
}
"
)
# 列印接收的數據
s
.
close
()
# 關閉socket
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
import
socket
host
=
socket
.
gethostname
()
# 主機的域名
port
=
2255
# 連接port編號
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
# 建立socket物件
s
.
bind
((
host
,
port
))
# 綁定IP和port
s
.
listen
()
# TCP監聽
print
(
"Server端 : waiting ..."
)
conn
,
addr
=
s
.
accept
()
# 被動接收客戶連線
print
(
"Server端:已經連線"
)
msg
=
conn
.
recv
(
1024
).
decode
()
# 接收客戶的數據
while
msg
!=
"bye"
:
if
msg
:
print
(
f"顯示收到內容 :
{
msg
}
"
)
# 輸出Client訊息
mydata
=
input
(
"輸入傳送內容 : "
)
# 讀取輸入內容
conn
.
send
(
mydata
.
encode
())
# 編碼為bytes後輸出
if
mydata
==
"bye"
:
# 如果是bye
break
# 離開while迴圈
print
(
"Server端 : waiting ..."
)
msg
=
conn
.
recv
(
1024
).
decode
()
# 讀取輸入內容
conn
.
close
()
s
.
close
()
print
(
"------------------------------------------------------------"
)
# 60個
import
socket
host
=
socket
.
gethostname
()
# 主機的域名
port
=
2255
# 連接port編號
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
# 建立socket物件
s
.
connect
((
host
,
port
))
# 執行連線
print
(
"Client端 : 已經連線"
)
msg
=
""
# 主要是初次連線用
while
msg
!=
"bye"
:
mydata
=
input
(
"輸入傳送內容 : "
)
# 讀取輸入內容
s
.
send
(
mydata
.
encode
())
# 編碼為bytes後輸出
if
mydata
==
"bye"
:
# 如果是bye
break
# 離開while迴圈
print
(
"Client端 : waiting ..."
)
msg
=
s
.
recv
(
1024
).
decode
()
# 讀取輸入內容
print
(
f"顯示收到內容 :
{
msg
}
"
)
# 輸出Server訊息
s
.
close
()
print
(
"------------------------------------------------------------"
)
# 60個
import
socket
host
=
host
=
"127.0.0.1"
# 主機的域名
port
=
2255
# 連接port編號
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
# 建立socket物件
s
.
bind
((
host
,
port
))
# 綁定IP和port
print
(
"Server : 綁定完成"
)
print
(
"Waiting ..."
)
f
,
addr
=
s
.
recvfrom
(
1024
)
# 被動接收客戶數據
print
(
f"received from
{
addr
}
"
)
c
=
f
.
decode
()
# 將bytes資料解碼
c
=
(
float
(
f
)
-
32
)
*
5
/
9
# 轉成攝氏溫度
mydata
=
str
(
c
)
# 轉成字串
s
.
sendto
(
mydata
.
encode
(),
addr
)
# bytes資料編碼再傳送
s
.
close
()
print
(
"------------------------------------------------------------"
)
# 60個
import
socket
host
=
host
=
"127.0.0.1"
# 主機的域名
port
=
2255
# 連接port編號
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
# 建立socket物件
mydata
=
input
(
"請輸入華氏溫度 : "
)
s
.
sendto
(
mydata
.
encode
(), (
host
,
port
))
# 送給伺服器
print
(
f"攝氏溫度 :
{
s
.
recv
(
1024
).
decode
()
}
"
)
s
.
close
()
print
(
"------------------------------------------------------------"
)
# 60個
from
flask
import
Flask
app
=
Flask
(
__name__
)
@
app
.
route
(
"/"
)
def
hello
():
return
"歡迎來到深智數位"
if
__name__
==
"__main__"
:
app
.
run
()
print
(
"------------------------------------------------------------"
)
# 60個
from
flask
import
Flask
app
=
Flask
(
__name__
)
@
app
.
route
(
"/"
)
@
app
.
route
(
"/index"
)
@
app
.
route
(
"/hello"
)
def
hello
():
return
"歡迎來到深智數位"
if
__name__
==
"__main__"
:
app
.
run
()
print
(
"------------------------------------------------------------"
)
# 60個
from
flask
import
Flask
app
=
Flask
(
__name__
)
@
app
.
route
(
"/<name>"
)
def
hello
(
name
):
return
f"Hi!
{
name
}
歡迎光臨深智數位"
if
__name__
==
"__main__"
:
app
.
run
()
print
(
"------------------------------------------------------------"
)
# 60個
from
flask
import
Flask
,
request
,
abort
import
os
from
linebot
import
LineBotApi
,
WebhookHandler
from
linebot
.
exceptions
import
InvalidSignatureError
from
linebot
.
models
import
(
MessageEvent
,
TextMessage
,
TextSendMessage
,
)
app
=
Flask
(
__name__
)
line_bot_api
=
LineBotApi
(
os
.
getenv
(
"Channel_token"
))
handler
=
WebhookHandler
(
os
.
getenv
(
"Channel_secret"
))
# 收Line 訊息
@
app
.
route
(
"/"
,
methods
=
[
"POST"
])
def
callback
():
# get X-Line-Signature header value
signature
=
request
.
headers
[
"X-Line-Signature"
]
# get request body as text
body
=
request
.
get_data
(
as_text
=
True
)
app
.
logger
.
info
(
"Request body: "
+
body
)
# handle webhook body
try
:
handler
.
handle
(
body
,
signature
)
except
InvalidSignatureError
:
print
(
"Invalid signature. Please check your channel access token/channel secret."
)
abort
(
400
)
return
"OK"
# Echo 回應, 相當於學你說話
@
handler
.
add
(
MessageEvent
,
message
=
TextMessage
)
def
handle_message
(
event
):
line_bot_api
.
reply_message
(
event
.
reply_token
,
TextSendMessage
(
text
=
event
.
message
.
text
)
)
if
__name__
==
"__main__"
:
app
.
run
(
host
=
"0.0.0.0"
,
port
=
5000
)
print
(
"------------------------------------------------------------"
)
# 60個
from
flask
import
Flask
,
request
,
abort
import
os
from
linebot
import
LineBotApi
,
WebhookHandler
from
linebot
.
exceptions
import
InvalidSignatureError
from
linebot
.
models
import
MessageEvent
,
TextMessage
,
TextSendMessage
import
openai
app
=
Flask
(
__name__
)
# 設定Line API & Webhook
line_bot_api
=
LineBotApi
(
os
.
getenv
(
"Channel_token"
))
handler
=
WebhookHandler
(
os
.
getenv
(
"Channel_secret"
))
# 設定OpenAI API
openai
.
api_key
=
os
.
getenv
(
"OpenAI_key"
)
# 收Line 訊息
@
app
.
route
(
"/"
,
methods
=
[
"POST"
])
def
callback
():
signature
=
request
.
headers
[
"X-Line-Signature"
]
body
=
request
.
get_data
(
as_text
=
True
)
app
.
logger
.
info
(
"Request body: "
+
body
)
try
:
handler
.
handle
(
body
,
signature
)
except
InvalidSignatureError
:
print
(
"Invalid signature. Please check your channel access token/channel secret."
)
abort
(
400
)
return
"OK"
# 使用OpenAI回應Line訊息
@
handler
.
add
(
MessageEvent
,
message
=
TextMessage
)
def
handle_message
(
event
):
user_input
=
event
.
message
.
text
response
=
openai
.
ChatCompletion
.
create
(
model
=
"gpt-3.5-turbo"
,
messages
=
[
{
"role"
:
"system"
,
"content"
:
"你是深智公司客服人員"
},
{
"role"
:
"user"
,
"content"
:
user_input
},
],
max_tokens
=
150
,
# 限制回應token數
)
response
=
response
.
choices
[
0
].
message
[
"content"
]
line_bot_api
.
reply_message
(
event
.
reply_token
,
TextSendMessage
(
text
=
response
.
strip
().
replace
(
"
\n
"
,
""
))
)
if
__name__
==
"__main__"
:
app
.
run
(
host
=
"0.0.0.0"
,
port
=
5000
)
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"作業完成"
)
print
(
"------------------------------------------------------------"
)
# 60個
sys
.
exit
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
# 3030
print
(
"------------------------------"
)
# 30個
Back
|
FazBrowse Home
|
New Git URL