1. 程式人生 > >勇哥的第一個博客

勇哥的第一個博客

AI sock import lec server usr rip 請求 recvfrom

#!/usr/bin/env python
# -*- coding:utf-8 -*-

服務端
# import socket
# ip_port = (‘127.0.0.1‘,9999)
# sk = socket.socket()
# sk.bind(ip_port)
# sk.listen(5)
# while True:
# print("server waiting....")
# conn,address = sk.accept()
# client_data = conn.recv(1024)
# client_data_str = str(client_data,encoding= ‘utf-8‘)
# print(client_data_str)
# conn.sendall(bytes(‘不要回答,不要回答!!!‘,encoding= ‘utf-8‘))
# conn.close()



# 套接字文件描述符服務端
"""
import socket
ip_port = (‘127.0.0.1‘,9999)
sk = socket.socket()
sk.bind(ip_port)
while True:
data,(host,port) = sk.recvfrom(1027)
print(str(data,encoding= ‘utf-8‘))
sk.sendto(bytes(‘ok‘,encoding= ‘utf-8‘),(host,port))
"""



# 智能機器人服務端

"""
import socket
ip_port = (‘127.0.0.1‘,9999)
sk = socket.socket()
sk.bind(ip_port)
sk.listen(5)
print("服務端啟動")
while True:
conn,address = sk.accept()
conn.sendall(bytes(‘歡迎致電10086,請輸入1xxx,0轉人工服務:‘,encoding= ‘utf-8‘))
Flag = True
while Flag:
data_str = str(conn.recv(1024),encoding= ‘utf-8‘)
if data_str == ‘exit‘:
Flag = False
elif data_str == ‘0‘:
conn.sendall(bytes("通話可能會被錄音!!!",encoding= ‘utf-8‘))
else:
conn.sendall(bytes("請重新輸入!!!:",encoding= ‘utf-8‘))

conn.close()
"""

# 利用select實現偽同時處理多個Socket客戶端請求:服務端


import socket
import select
sk1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sk1.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
sk1.bind((‘127.0.0.1‘,9999))
sk1.listen(5)
sk1.setblocking(0)

inputs = [sk1,]

while True:
r_list,w_list,e_list = select.select(inputs,[],inputs,1)
for r in r_list:
if sk1 == r:
print("accept")
request,address = r.accept()
print("服務器開啟")
request.setblocking(0)
inputs.append(request)
else:
received = r.recv(1024)
if received:
print("received:",received)

else:
inputs.remove(r)
sk1.close()




客戶端

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# import socket
# obj = socket.socket()
# ip_port = (‘127.0.0.1‘,9999)
# obj.connect(ip_port)
# obj.sendall(bytes("我是地球人",encoding= ‘utf-8‘))
# jieshou = obj.recv(1024)
# jieshou_str = str(jieshou,encoding= ‘utf-8‘)
# print(jieshou_str)
# obj.close()

# 套接字文件描述符客戶端

"""
import socket
ip_port = (‘127.0.0.1‘,9999)
sk = socket.socket()
while True:
inp = input(‘數據。。。。。‘).strip()
if inp == ‘exit‘:
break
sk.sendto(bytes(inp,encoding= ‘utf-8‘),ip_port)
data = sk.recvfrom(1024)
print(data)

sk.close()
"""


# 智能機器人客戶端

"""
import socket
ip_port = ("127.0.0.1",9999)
sk = socket.socket()
sk.connect(ip_port)

while True:
data = sk.recv(1024)
data_str = str(data,encoding= ‘utf-8‘)

print("receive",data_str)
while True:
inp = input("please input:")
inp_bytes = bytes(inp,encoding= ‘utf-8‘)
sk.sendall(inp_bytes)
if inp == ‘exit‘:
break

blak= sk.recv(1024)
blak_str = str(blak,encoding= ‘utf-8‘)
print(blak_str)

sk.close()
"""

# 利用select實現偽同時處理多個Socket客戶端請求:服務端

import socket
sk = socket.socket()
sk.connect((‘127.0.0.1‘,9999))
while True:
inp = input("請輸入:")
inp_bytes = bytes(inp,encoding=‘utf-8‘)
sk.sendall(inp_bytes)

sk.close()






























勇哥的第一個博客