python

requests的简单运用

使用pycharm获取requests包

ctrl+alt+s Project:pythonProject pythoninterpreter 点+号搜索

使用requests里的socket达成局域网内通讯,UDP

import time   #server
import socket
try:
s = socket.socket(type=socket.SOCK_DGRAM) # 创建类
hostname = socket.gethostname() # 获取自己的主机名
s.bind((hostname, 8888)) # 绑定主机号与端口号
msg,address=s.recvfrom(1024)
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())),
"服务端收到客户端:%s的消息:%s"%(address,msg.decode('utf-8')))
data='可以正常通讯'.encode('utf-8')
s.sendto(data,address)
while True:
a = input("发送消息(q = quit)")
if a != "q":
s.sendto(a.encode('utf-8'), address)
else:
break
msg,address=s.recvfrom(1024)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())),
"服务端收到客户端:%s的消息:%s" % (address, msg.decode('utf-8')))
s.close()
except Exception as e:
print("出错了", e) import socket #client
import time
try:
s=socket.socket(type=socket.SOCK_DGRAM)
hostname=socket.gethostname()
data='连接成功'.encode('utf-8')
s.sendto(data,(hostname,8888))
responese,address=s.recvfrom(1024)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),
'收到服务端%s的消息:%s'%(address,responese.decode('utf-8')))
n=0
while n<1000:
response,address = s.recvfrom(1024)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())),
"收到服务端%s的消息:%s" % (address, response.decode('utf-8')))
a = input('发送信息')
s.sendto(a.encode('utf-8'),address)
s.close()
except Exception as e:
print('出错了',e)

使用requests里的socket达成局域网内通讯,TCP

import socket   #server
import time
try:
s = socket.socket()
hostname = socket.gethostname()
# 绑定套接字地址
s.bind((hostname, 8888))
s.listen(5)
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '服务端准备完毕,等待客户端连接')
con, address = s.accept()
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),'客户端已连接上服务端,连接地址:', address)
message = '你好,我是服务端'
# 发送消息,必须对发送的内容进行编码
con.send(message.encode('utf-8'))
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '服务端发送:', message)
# 关闭套接字
con.close()
except Exception as e:
print('建立TCP服务端失败', e) import socket
import time
try:
s = socket.socket()
hostname = socket.gethostname()
s.connect((hostname, 8888))
response = s.recv(1024).decode('utf-8') # 接收服务端消息并解码
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '收到服务端消息:', response)
s.close() # 关闭套接字
except Exception as e:
print('建立TCP客户端失败', e)

requests模块里的get应用

import requests
word = input('请输入搜索内容')
pn = input('请输入页码')
p={'kw':word,"pn":pn}
url='https://tieba.baidu.com/f'
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0'}
re=requests.get(url,headers=headers,params=p)
print(re.content.decode('utf-8'))

输入图片地址将结尾改成.jpg可以爬图片

request模块里的post应用

这里使用post去给百度翻译传参数

import requests
import json
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0"}
url='https://fanyi.baidu.com/sug'
while True:
word = input('百度翻译:')
if word.lower() == 'quit':
break
data={"kw":word}
re=requests.post(url,headers=headers,data=data) #地址,伪装的浏览器以及数据
re_obj=re.json() #把json格式写进re_obj
print(word+":"+re_obj['data'][0]['v']) #指定re_obj里的特定值
filename=word+'.json' #设置文件名
fp=open(filename,"w",encoding='utf-8') #以utf-8写入文件
json.dump(re_obj,fp=fp,ensure_ascii=False) #不允许json以ascii码的形式写入文件

get是在url地址上加参数,post是在页面里面的文本栏加参数

最新文章

  1. Linux基础知识
  2. Model1
  3. Codeforces Round #197 (Div. 2)
  4. python中将两个list合并为字典
  5. Java单例模式再加强——按组多单例
  6. JAVA高并发
  7. 高性能消息队列 CKafka 核心原理介绍(上)
  8. 关于xlrd处理合并单元格
  9. 【公众号系列】两分钟学会SAP F1技巧
  10. 常见的CSS
  11. 判断某个元素是否存在于某个 js 数组中
  12. Python 事件驱动与异步IO
  13. 罗克韦尔 Allen-Bradley MicroLogix 1400 查看、设置IP
  14. mariadb的flashback到底怎么样???防误删可以,但算不上真正的闪回--再看mariadb 10.3的System-Versioned Tables
  15. mybatis的注解开发之三种动态sql
  16. c++11 委托构造
  17. std::string 赋值为nullptr引起程序崩溃
  18. Using shared access signatures (SAS) From Microsoft
  19. 【BZOJ】2724: [Violet 6]蒲公英
  20. 对固态硬盘ssd进行4k对齐

热门文章

  1. 使用Python定时清理运行超时的pdflatex僵尸进程
  2. 2基因组间鉴定SV
  3. mysql-select as
  4. 【Redis集群原理专题】分析一下相关的Redis集群模式下的脑裂问题!
  5. matplotlib以对象方式绘制子图
  6. 一个简单的BypassUAC编写
  7. A Child&#39;s History of England.34
  8. Kotlin 学习(1)
  9. Can static functions be virtual in C++?
  10. java内存管理的小技巧