相关疑难解决方法(0)

Python 在发送前请求查看

有人可以为我提供一种方法来查看我在将其发送到服务器之前生成的请求,这是代码:

import requests
import urllib2
import logging

from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

url = "https://10.1.1.254:4081/admin/api/jsonrpc/"
session = requests.Session()

data = {
    "jsonrpc": "2.0", "id": 1, "method": "Session.login",
    "params": {"userName": "test", "password":"test123"}
}
r = session.post(url, json=data, verify=False)

data = {"jsonrpc": "2.0", "id":3, "method":"Session.logout"}
r = session.post(url, json=data, verify=False)
Run Code Online (Sandbox Code Playgroud)

所以我想要的是在 Python 发送它之前用 session.post 发送该请求。

python python-requests

9
推荐指数
2
解决办法
7956
查看次数

Python 请求得到一个应该可以正常工作的 URL 的错误页面

我正在尝试使用 python 脚本来抓取政府网站上的一些页面。我让它访问一个在我的网络浏览器中加载正常网页的 URL,但由于某种原因,该脚本得到一个“访问被拒绝”页面而不是预期的页面。

此外,这个“拒绝访问”错误与我在政府网站上见过的任何错误都不同。除了我的 python 脚本之外,我无法通过任何方式实现此错误。

这是我的脚本的精简版本(它相当大,所以我删除了我认为不相关的部分):

import requests

headers = {
    'Accept': "*/*",
    'User-Agent': "nyc_contractors.py",
    'X-Love': "hey sysadmin! you're awesome! <3"
}

print "and we're off!"

qLicensetype="C"
qBizname = "a"

baseUrl = "http://a810-bisweb.nyc.gov/bisweb/ResultsByNameServlet?bizname="+qBizname+"&licensetype="+qLicensetype
nextUrl = baseUrl

while nextUrl != None:

    print
    print "URL:", nextUrl

    r = requests.get(nextUrl, headers=headers)
    nextUrl = None # kill the url (if there's a next page, we'll restore the url later)
    print "actual url:",r.url

    lines = r.text.splitlines()

    for line in …
Run Code Online (Sandbox Code Playgroud)

python curl http python-requests

5
推荐指数
1
解决办法
5177
查看次数

Twint: CRITICAL:root:twint.feed:Follow:IndexError for any call

import twint
import os, requests, re, time

c = twint.Config()
c.Username = <anyusername> #Replace with an actual uname in quotes
c.Store_object = True
c.Limit = 10
try:
    twint.run.Followers(c)
except:
    print("Unexpected error:", sys.exc_info()[0])
f = twint.output.follows_list
print(f)
Run Code Online (Sandbox Code Playgroud)

输出

CRITICAL:root:twint.feed:Follow:IndexError
[]
Run Code Online (Sandbox Code Playgroud)

完成了

pip install twint
pip install --upgrade -e git+https://github.com/twintproject/twint.git@origin/master#egg=twint  
Run Code Online (Sandbox Code Playgroud)

谷歌搜索,很多人都遇到过这个错误 - 但我真的找不到解决方案

运行 twint 命令行也会出现同样的错误

twint -u <uname> --followers
CRITICAL:root:twint.feed:Follow:IndexError
Run Code Online (Sandbox Code Playgroud)

这不仅是追随者。我尝试任何操作,都会遇到类似的错误。

在 Windows 10
Twint上运行 Python 3.8.1 - 最新版本 - 2.1.21

python twitter exception

2
推荐指数
1
解决办法
2373
查看次数

Python HTTP 请求和调试级别记录到日志文件

我很难在 HTTP 请求的日志文件中获取调试级别的日志,例如来自控制台的请求:

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): URI:443
DEBUG:urllib3.connectionpool:URL:443 "POST /endpoint HTTP/1.1" 200 None
Run Code Online (Sandbox Code Playgroud)

对于下面的代码:

import logging
from logging.handlers import TimedRotatingFileHandler
_logger = logging.getLogger(__name__)

    def setup_logging(loglevel):
        logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")

        if loglevel is not None:
            if loglevel == 10:
                  http.client.HTTPConnection.debuglevel = 1
            logformat = "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s"
            logging.basicConfig(level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S")

        fileHandler = logging.handlers.TimedRotatingFileHandler("{0}/{1}.log".format(logPath, logFileName), when="midnight")
        fileHandler.setFormatter(logFormatter)
        _logger.setLevel(logging.DEBUG)
        _logger.addHandler(fileHandler)
Run Code Online (Sandbox Code Playgroud)

当我用logging.DEBUG日志文件调用它时,它将只包含我将在代码中指定的任何内容_logger.info_logger.debug与控制台日志输出类似的内容。

附注。示例代码如何调用它:

def main(args):
    args = parse_args(args)
    cfg = config(args.env) …
Run Code Online (Sandbox Code Playgroud)

python logging python-3.x

1
推荐指数
1
解决办法
3373
查看次数

标签 统计

python ×4

python-requests ×2

curl ×1

exception ×1

http ×1

logging ×1

python-3.x ×1

twitter ×1