requests

Posted realadmin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了requests相关的知识,希望对你有一定的参考价值。

import requests

import time

url = ‘http://www.google.com.hk‘

print(time.strftime(‘%Y-%m-%d %H:%M:%S‘))
try:
    html = requests.get(url, timeout=5).text
    print(html)
    print(‘success‘)
except requests.exceptions.RequestException as e:
    print(111,e)

print(time.strftime(‘%Y-%m-%d %H:%M:%S‘))

  

2019-12-18 23:55:23
111 HTTPConnectionPool(host=‘www.google.com.hk‘, port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x10c70fc88>, ‘Connection to www.google.com.hk timed out. (connect timeout=5)‘))
2019-12-18 23:55:28

https://www.cnblogs.com/jiu0821/p/6275685.html

 

from django.db import models


# Create your models here.
import os

def path(instance,file):
return os.path.join("file",instance.title)
# return os.path.join("file",instance.title+‘_‘+file.name)

class Author(models.Model):
name = models.CharField(max_length=30)
age = models.IntegerField()

class Meta:
verbose_name = "作者表"
verbose_name_plural = verbose_name

def __str__(self):
return self.name


class Book(models.Model):
title = models.CharField(max_length=100)
price = models.IntegerField()
authors = models.ManyToManyField(Author)
code_file = models.FileField(upload_to=path,max_length=256,blank=True,null=True)

class Meta:
verbose_name = "图书表"
verbose_name_plural = verbose_name

def __str__(self):
return self.title
# coding:utf-8
from django.shortcuts import render, HttpResponse
from django.http import JsonResponse

# Create your views here.

from .models import Book, Author
import json
import requests
from django.conf import settings
import os
from django.core.files.base import ContentFile

def test(request):
    url = r‘/Users/just/PycharmProjects/test1/app01/101010.docx‘
    obj = Book.objects.filter(title="小红和小明的书").first()
    obj.code_file=r‘/Users/just/PycharmProjects/test1/app01/101010.docx‘
    obj.save()


    # url = r‘https://www.so.com/?src=so.com‘
    # file = requests.get(url)
    # file = file.text
    # path = os.path.join(settings.STATIC_URL,‘file‘,obj.title+"_"+str(obj.id))
    # if not os.path.exists(path):
    #     os.mkdir(path)
    # path = path+r"/test.txt"
    # with open(path,‘wb‘) as f:
    #     f.write(r.content)
    data = {"code_file":obj.code_file.url,"code":1000}
    return JsonResponse(data)

  {"code_file": "Users/just/PycharmProjects/test1/app01/101010.docx", "code": 1000}

 

    obj = Book.objects.filter(id=2).first()
    url = r‘https://www.so.com/?src=so.com‘
    file = requests.get(url)
    path = os.path.join(settings.BASE_DIR,‘file‘,obj.title)
    if not os.path.exists(path):
        os.makedirs(path)
    path = path+os.sep+"test.txt"
    print(path)
    with open(path,‘wb‘) as f:
        f.write(file.content)

    obj.code_file=path
    obj.save()
    data = {"code_file":obj.code_file.url,"code":1000}
    return JsonResponse(data)

  {"code_file": "Users/just/PycharmProjects/test1/file/%E5%B0%8F%E7%BA%A2%E5%92%8C%E5%B0%8F%E6%9D%8E%E7%9A%84%E4%B9%A6/test.txt", "code": 1000}

以上是关于requests的主要内容,如果未能解决你的问题,请参考以下文章

C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段

推进学说代码片段

Motan在服务provider端用于处理request的线程池

[未解决问题记录]python asyncio+aiohttp出现Exception ignored:RuntimeError('Event loop is closed')(代码片段

你如何在 python 中处理 graphql 查询和片段?

request.getParameter讲解