简体   繁体   English

Python 3 urllib HTTP错误403:禁止使用参数的localhost

[英]Python 3 urllib HTTP Error 403: FORBIDDEN with localhost using params

I have been trying to make a POST request in this way: 我一直试图以这种方式发出POST请求:

def simular(request):
    data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
    data = data.encode('utf-8')
    request = urllib.request.Request("http://localhost:8000/aguaman/actualizar/")
    # adding charset parameter to the Content-Type header.
    request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
    f = urllib.request.urlopen(request, data)
    return HttpResponse("se está simulando la cosa para que funcione: " + str(data))

But I am getting the error 403 forbidden 但是我收到了禁止的错误403

Then I try to do the same request but I am not using the params so it would be somethink like this: 然后,我尝试执行相同的请求,但是我没有使用参数,因此可能会像这样:

def simular(request):
    data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
    data = data.encode('utf-8')
    request = urllib.request.Request("http://localhost:8000/aguaman/actualizar/")
    # adding charset parameter to the Content-Type header.
    request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
    f = urllib.request.urlopen(request)
    return HttpResponse("se está simulando la cosa para que funcione: " + str(data))

And it works, So I would like to know if something is missing, or maybe I cannot use urllib in the way I am doing for local files. 它可以正常工作,所以我想知道是否丢失了某些东西,或者我无法以对本地文件的方式使用urllib。

after reading in several places, I found that changing header can help, I already did that and tried to use: 在多个地方阅读后,我发现更改标头可以提供帮助,我已经做到了,并尝试使用:

hdr = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
    'Accept-Encoding': 'none',
    'Accept-Language': 'en-US,en;q=0.8',
    'Connection': 'keep-alive'}
    req = urllib.request.Request(url, data, hdr)

But is useless, same result. 但是是没有用的,同样的结果。 does anyone can explain the reason? 有谁能解释原因? thanks 谢谢

After making a lot of different changes I started using @csrf_exempt before the views definition, so at the end I finished with these code 进行了许多不同的更改之后,我开始在视图定义之前使用@csrf_exempt,因此最后我完成了这些代码

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def simular(request):
    data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
    data = data.encode('utf-8')
    request = urllib.request.Request("http://localhost:8000/aguaman/actualizar/")
    # adding charset parameter to the Content-Type header.
    request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
    f = urllib.request.urlopen(request, data)
    return HttpResponse("se está simulando la cosa para que funcione: " + str(data))`

doing this, I was disabling the django security request for this view, I tried to test and the error continued, so I shutted down the server, and turned it on again, and starts working. 这样做时,我为此视图禁用了django安全请求,我尝试进行测试并且错误继续,因此我关闭了服务器,然后再次打开服务器,然后开始工作。

Maybe there is something in the server that checks those kind of variables in the app before start running them, so now I can continue working. 也许服务器中有一些东西在开始运行它们之前先检查应用程序中的这些变量,所以现在我可以继续工作了。 thanks ! 谢谢 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM