我正在向网络服务发布一些参数,如下所示:
requests.post("http://**.***.**.**:****/MyLogin/services/DBConnection/callLoginProcedure?inputPhoneNumber=" + phone + "&inputPassword=" + password)
"callLoginProcedure"返回一个整数值,但我无法获得该值。如何获取此返回值?
views.py:
def index(request):
post = request.POST.copy()
if post.get('login_button'):
phone = post.get('phone_num')
password = post.get('password')
requests.post("http://**.***.**.**:****/MyLogin/services/DBConnection/callLoginProcedure?
inputPhoneNumber=" + phone + "&inputPassword=" + password)
r = request.GET.get("return", "-1")
# if r == 1:
# messages.info(request, 'successful!')
# else:
# messages.info(request, 'unsuccessful!')
messages.info(request, r)
return render(request, 'login/index.html')
urls.py:
urlpatterns = [
url(r'^$', views.index, name = 'index'),
path(r'^$', views.index, name = 'script'),
]
编辑:
我的问题已解决,但是调用 requests.post 函数时出现代理错误。我不知道如何解决它。堆栈跟踪如下所示:
Request Method: POST
Request URL: http://localhost:8000/login/
Django Version: 2.2.3
Python Version: 3.7.3
Installed Applications:
['login',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware']
Traceback:
File "C:Program FilesPython37libsite-packagesurllib3connectionpool.py" in urlopen
603. chunked=chunked)
File "C:Program FilesPython37libsite-packagesurllib3connectionpool.py" in _make_request
344. self._validate_conn(conn)
File "C:Program FilesPython37libsite-packagesurllib3connectionpool.py" in _validate_conn
843. conn.connect()
File "C:Program FilesPython37libsite-packagesurllib3connection.py" in connect
370. ssl_context=context)
File "C:Program FilesPython37libsite-packagesurllib3utilssl_.py" in ssl_wrap_socket
368. return context.wrap_socket(sock)
File "C:Program FilesPython37libssl.py" in wrap_socket
412. session=session
File "C:Program FilesPython37libssl.py" in _create
853. self.do_handshake()
File "C:Program FilesPython37libssl.py" in do_handshake
1117. self._sslobj.do_handshake()
During handling of the above exception ([SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)), another exception occurred:
File "C:Program FilesPython37libsite-packagesrequestsadapters.py" in send
449. timeout=timeout
File "C:Program FilesPython37libsite-packagesurllib3connectionpool.py" in urlopen
641. _stacktrace=sys.exc_info()[2])
File "C:Program FilesPython37libsite-packagesurllib3utilretry.py" in increment
399. raise MaxRetryError(_pool, url, error or ResponseError(cause))
During handling of the above exception (HTTPSConnectionPool(***): Max retries exceeded with url: **** (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)')))), another exception occurred:
File "C:Program FilesPython37libsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:Program FilesPython37libsite-packagesdjangocorehandlersbase.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:Program FilesPython37libsite-packagesdjangocorehandlersbase.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersTOLGADesktopPythonWebProjectsWebLoginloginviews.py" in index
53. response = requests.post('{}?{}'.format(endpoint, qd.urlencode()))
File "C:Program FilesPython37libsite-packagesrequestsapi.py" in post
116. return request('post', url, data=data, json=json, **kwargs)
File "C:Program FilesPython37libsite-packagesrequestsapi.py" in request
60. return session.request(method=method, url=url, **kwargs)
File "C:Program FilesPython37libsite-packagesrequestssessions.py" in request
533. resp = self.send(prep, **send_kwargs)
File "C:Program FilesPython37libsite-packagesrequestssessions.py" in send
646. r = adapter.send(request, **kwargs)
File "C:Program FilesPython37libsite-packagesrequestsadapters.py" in send
514. raise SSLError(e, request=request)
Exception Type: SSLError at /login/
首先,我强烈建议你不要自己对URL进行编码,而是使用Django的QueryDict
,例如:
from django.http import QueryDict
qd = QueryDict(mutable=True)
qd.update(inputPhoneNumber=phone, inputPassword=password)
例如:
>>> qd = QueryDict(mutable=True)
>>> qd.update(inputPhoneNumber=phone, inputPassword=password)
>>> qd.urlencode()
'inputPhoneNumber=0015550183&inputPassword=some_password'
接下来,您应该读出 POST 请求的响应,例如:
from django.http import QueryDict
from json.decoder import JSONDecodeError
import requests
endpoint = 'http://68.183.75.84:8080/Calculator/services/DBConnection/callLoginProcedure'
def index(request):
post = request.POST
if request.POST.get('login_button'):
qd = QueryDict(mutable=True)
qd.update(
inputPhoneNumber=request.POST.get('phone_num'),
inputPassword=request.POST.get('password')
)
response= requests.post('{}?{}'.format(endpoint, qd.urlencode()))
try:
result =response.json()
else JSONDecodeError:
result =-1
# ...
return render(request, 'login/index.html')