勇哥的第一个博客
Posted 他人能者亦能,他人不能者吾亦能
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了勇哥的第一个博客相关的知识,希望对你有一定的参考价值。
#!/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()
以上是关于勇哥的第一个博客的主要内容,如果未能解决你的问题,请参考以下文章
进一步了解XPath(利用XPath爬取飞哥的博客)python爬虫入门进阶(04)