Django : error: [Errno 10053] An established connection was aborted by the software in your host machine - ajax

I got error when i perform ajax request. Without authentication lines user = authenticate(username = user_username, password = user_password) in the views.py, success function is called. And if i add, error function is called with Errno 10053.
I am using MySql in Wamp. Why it is happening ?
views.py
class LoginVerify(View):
print('login')
def post(self,request,*args,**kwargs):
if request.is_ajax():
print("post called")
user_email = request.POST.get('email',False)
user_password = request.POST.get('pswd',False)
print(user_email)
try:
user_username = User.objects.get(email=user_email).username
user = authenticate(username = user_username, password = user_password)
except:
print("error occured")
return HttpResponse("response form server")
def get(self, request,*args,**kwargs):
print("get method")
return render(request,'feeds/feeds_home.html')
ajax request:
$(document).ready(function(){
$("#submit").on("click",function(){
var $email = $("#signin-email").val();
var $pswd = $("#signin-password").val();
alert($pswd);
$.ajax({
url : '{% url "feeds:login_view" %}',
type: "POST",
data: {csrfmiddlewaretoken :"{{ csrf_token }}", pswd : $pswd, email: $email},
success: function(data){
location.reload();
},
error: function(){
alert("fails");
}
});
});
Tracebrack
post called
vivek.ananthan.m.s#gmail.com
[19/Apr/2015 11:10:22] "POST / HTTP/1.1" 200 12
Traceback (most recent call last):
File "C:\Python27\lib\wsgiref\handlers.py", line 86, in run
self.finish_response()
File "C:\Python27\lib\wsgiref\handlers.py", line 127, in finish_response
self.write(data)
File "C:\Python27\lib\wsgiref\handlers.py", line 210, in write
self.send_headers()
File "C:\Python27\lib\wsgiref\handlers.py", line 268, in send_headers
self.send_preamble()
File "C:\Python27\lib\wsgiref\handlers.py", line 192, in send_preamble
'Date: %s\r\n' % format_date_time(time.time())
File "C:\Python27\lib\socket.py", line 324, in write
self.flush()
File "C:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
Traceback (most recent call last):
File "C:\Python27\lib\SocketServer.py", line 582, in process_request_thread
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\core\servers\basehttp.py", line 129, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "C:\Python27\lib\SocketServer.py", line 640, in __init__
self.finish()
File "C:\Python27\lib\SocketServer.py", line 693, in finish
self.wfile.flush()
File "C:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52490)
Please explain where i made the mistake and why it is happening.
Thanks in advance !!

Reference: https://stackoverflow.com/a/17854758/3940406
From the Windows Sockets Error Codes list:
WSAECONNABORTED 10053 Software caused connection abort. An established
connection was aborted by the software in your host computer, possibly
due to a data transmission time-out or protocol error.
There was a timeout or other network-level error. This is your operating system closing the socket, nothing to do with Python, django or Flask, really.
It could be the remote browser stopped responding, the network connection died, or a firewall closed the connection because it was open too long, or any other number of reasons.

I came by the question when I was trying to research a problem with regards to running a mysql code using the pymysql as the python sql client. I happen to have this same issue. Renaming the config setting for mysql and then restarting your system or running mysql server. Works to fix this issue

Related

Getting this ConnectionResetError: [Errno 54] Connection reset by peer when hitting GET request in API

I am using angular with drf whenever i send some GET request i got response in my console like this and the error in console start increasing
Exception happened during processing of request from ('192.168.43.129', 58645)
Traceback (most recent call last):
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 720, in __init__
self.handle()
File "/Users/chip/Documents/Git/apis_gifola/venv/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 171, in handle
self.handle_one_request()
File "/Users/chip/Documents/Git/apis_gifola/venv/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 179, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer

bot.get_me() doesn't work and raises an error

I can manually interact with the bot through url. For example when I send a request to api.telegram.com/bot-token/getMe
the bot's basic info is returned I even get correct results using requests library in python shell but when I try bot.get_me() in the python shell it doesn't work and says this
Traceback (most recent call last):
File "C:\Users\YM\AppData\Local\Programs\Python\Python38-32\lib\site-packages\
telegram\vendor\ptb_urllib3\urllib3\connection.py", line 140, in _new_conn
conn = connection.create_connection(
File "C:\Users\YM\AppData\Local\Programs\Python\Python38-32\lib\site-packages\
telegram\vendor\ptb_urllib3\urllib3\util\connection.py", line 83, in create_conn
ection
raise err
File "C:\Users\YM\AppData\Local\Programs\Python\Python38-32\lib\site-packages\
telegram\vendor\ptb_urllib3\urllib3\util\connection.py", line 73, in create_conn
ection
sock.connect(sa)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\YM\AppData\Local\Programs\Python\Python38-32\lib\site-packages\
telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 614, in urlopen
httplib_response = self._make_request(conn, method, url,
File "C:\Users\YM\AppData\Local\Programs\Python\Python38-32\lib\site-packages\
telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 360, in _make_reque
st
self._validate_conn(conn)
File "C:\Users\YM\AppData\Local\Programs\Python\Python38-32\lib\site-packages\
telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 857, in _validate_c
onn
super(HTTPSConnectionPool, self)._validate_conn(conn)
File "C:\Users\YM\AppData\Local\Programs\Python\Python38-32\lib\site-packages\
telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 289, in _validate_c
onn
conn.connect()
File "C:\Users\YM\AppData\Local\Programs\Python\Python38-32\lib\site-packages\
telegram\vendor\ptb_urllib3\urllib3\connection.py", line 284, in connect
conn = self._new_conn()
File "C:\Users\YM\AppData\Local\Programs\Python\Python38-32\lib\site-packages\
telegram\vendor\ptb_urllib3\urllib3\connection.py", line 144, in _new_conn
raise ConnectTimeoutError(
telegram.vendor.ptb_urllib3.urllib3.exceptions.ConnectTimeoutError: (<telegram.v
endor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x024257F
0>, 'Connection to api.telegram.org timed out. (connect timeout=5.0)')
Looks like you're having problems with your internet connection, i.e. the request could not be finished within the timeout of 5 seconds. Keep in mind that a lot of problems can happen unexpectedly in networking. In fact python-telegram-bot has a wiki page dedicated to that topic. Ofc you could first try to simply increase the timeout, e.g. by passing timeout=<some_value_>5> to get_me.

How to set socks5 proxy in tweepy?

To ssh login my vps,the codes can run successfully.
import tweepy
consumer_key="xxx"
consumer_secret="xxx"
access_token="xxx"
access_token_secret="xxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)
for info in tweets:
print(info.text)
Set all software related with proxy, and test socks proxy.
url="https://www.youtube.com/watch?v=l9AC98amjSA"
youtube-dl --proxy socks5://127.0.0.1:1080 $url -o /tmp/downName
It is verified that the video on youtube can be downloaded with socks5 proxy.
All software related with proxy are in good status.
Now i want to set socks5 proxy in my local pc,only one line was changed based on the above codes.
api = tweepy.API(auth,proxy="127.0.0.1:1080")
The error info is as following.
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 588, in urlopen
self._prepare_proxy(conn)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 803, in _prepare_proxy
conn.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 291, in connect
self._tunnel()
File "/usr/lib/python3.5/http/client.py", line 827, in _tunnel
(version, code, message) = response._read_status()
File "/usr/lib/python3.5/http/client.py", line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.5/socket.py", line 576, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out
It is no use also to add socks proxy statement as following.
import urllib.request
proxy_support = urllib.request.ProxyHandler({'sock5': 'localhost:1080'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
import tweepy
consumer_key="xxx"
consumer_secret="xxx"
access_token="xxx"
access_token_secret="xxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth) # or api = tweepy.API(auth,proxy="127.0.0.1:1080")
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)
for info in tweets:
print(info.text)
How to set socks5 proxy in tweepy?
According to Tarun Lalwani,package installed and codes changed ,still don't work.
sudo pip3 install -U requests[socks]
Set all related proxy software.
sudo netstat -anlp |grep 1080
tcp 0 0 127.0.0.1:1080 0.0.0.0:* LISTEN 1465/python3
udp 0 0 127.0.0.1:1080 0.0.0.0:* 1465/python3
Then enter into python3 console.
import tweepy
consumer_key="xxx"
consumer_secret="xxx"
access_token="xxx"
access_token_secret="xxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,proxy="socks5://127.0.0.1:1080")
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)
An other error info occur.
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/urllib3/contrib/pyopenssl.py", line 441, in wrap_socket
cnx.do_handshake()
File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1716, in do_handshake
self._raise_ssl_error(self._ssl, result)
File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1431, in _raise_ssl_error
raise WantReadError()
OpenSSL.SSL.WantReadError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 346, in _make_request
self._validate_conn(conn)
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 850, in _validate_conn
conn.connect()
File "/usr/local/lib/python3.5/dist-packages/urllib3/connection.py", line 326, in connect
ssl_context=context)
File "/usr/local/lib/python3.5/dist-packages/urllib3/util/ssl_.py", line 329, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/usr/local/lib/python3.5/dist-packages/urllib3/contrib/pyopenssl.py", line 445, in wrap_socket
raise timeout('select timed out')
socket.timeout: select timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 440, in send
timeout=timeout
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 639, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/local/lib/python3.5/dist-packages/urllib3/util/retry.py", line 357, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/local/lib/python3.5/dist-packages/urllib3/packages/six.py", line 686, in reraise
raise value
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 349, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 309, in _raise_timeout
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: SOCKSHTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out. (read timeout=60)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tweepy/binder.py", line 187, in execute
proxies=self.api.proxy)
File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 521, in send
raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: SOCKSHTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out. (read timeout=60)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/tweepy/binder.py", line 245, in _call
return method.execute()
File "/usr/local/lib/python3.5/dist-packages/tweepy/binder.py", line 189, in execute
raise TweepError('Failed to send request: %s' % e)
tweepy.error.TweepError: Failed to send request: SOCKSHTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out. (read timeout=60)
Two things. One you need the requests[socks] package
pip install requests[socks]
Next you need to use the proxy in socks format
api = tweepy.API(auth,proxy="socks5://127.0.0.1:1080")

geocoder Service error in python

I am having a service error as I am trying to run geocoders from geopy to get a location coordinates in python.I have set my proxy config, given it an api_key.
Im not sure why I am getting this error. From the search that I have done on this, it seems that this might be a proxy issue but I have that set up already.
What could be the problem here?
This is my code :
from geopy import geocoders
proxies={'http': 'http://location:port', 'https': 'http://localhost:port'}
api_key = '.......'
g = geocoders.GoogleV3(api_key=api_key,proxies=proxies, timeout=10)
location = 'Mountain View, CA'
try:
place, (lat, lng) = g.geocode(location)
except ValueError as error_message:
print("Error: geocode failed on input %s with message %s" % (location, error_message))
this is my error output:
Traceback (most recent call last):
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 1254, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1151, in _send_request
self.endheaders(body)
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1260, in connect
server_hostname=server_hostname)
File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 377, in wrap_socket
_context=self)
File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 752, in __init__
self.do_handshake()
File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 988, in do_handshake
self._sslobj.do_handshake()
File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 633, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:645)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/aqm1152/anaconda/lib/python3.5/site-packages/geopy/geocoders/base.py", line 143, in _call_geocoder
page = requester(req, timeout=(timeout or self.timeout), **kwargs)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 466, in open
response = self._open(req, data)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 484, in _open
'_open', req)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 444, in _call_chain
result = func(*args)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 1297, in https_open
context=self._context, check_hostname=self._check_hostname)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 1256, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error EOF occurred in violation of protocol (_ssl.c:645)>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/aqm1152/Documents/TestingCode/ACERT/test_1.py", line 12, in <module>
place, (lat, lng) = g.geocode(location)
File "/Users/aqm1152/anaconda/lib/python3.5/site-packages/geopy/geocoders/googlev3.py", line 217, in geocode
self._call_geocoder(url, timeout=timeout), exactly_one
File "/Users/aqm1152/anaconda/lib/python3.5/site-packages/geopy/geocoders/base.py", line 171, in _call_geocoder
raise GeocoderServiceError(message)
geopy.exc.GeocoderServiceError: EOF occurred in violation of protocol (_ssl.c:645)
I modified your code as below, as I don't use proxy and apply an API, and it works in my machine.
from geopy import geocoders
#proxies={'http': 'http://location:port', 'https': 'http://localhost:port'}
#api_key = '.......'
g = geocoders.GoogleV3()
location = 'Mountain View, CA'
try:
place, (lat, lng) = g.geocode(location)
except ValueError as error_message:
print("Error: geocode failed on input %s with message %s" % (location, error_message))
print (place, lat, lng)
Here is the result.
Mountain View, CA, USA 37.3860517 -122.0838511
From the trackback, it looks all the errors are related to SSL. You may try to add below code at the beginning to disable SSL Certification verification as an alternative.
import ssl
# Disable SSL certificate verification
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
I have encountered similar problem in Anaconda running Python 2.7 in a env with the root running Python 3.6. The problem wasn't encountered in Anaconda with both root and env running Python 2.7 in another machine.
In addition, this SO post about proxy server set up for geopy using os module may be good reference.
Hope this help.

werkzeug server failed to handle request

I'm trying to run this gevent server
from gevent import pywsgi
from Index import application
import paste.urlparser
import os
# http server: serves up static files
print "static files",os.path.dirname(__file__)
print "serving on localhost:8000"
http_server = pywsgi.WSGIServer(
('', 8000),
paste.urlparser.StaticURLParser(os.path.dirname(__file__)))
print 'Serving on https://127.0.0.1:4000'
server = pywsgi.WSGIServer(('0.0.0.0', 4000), application,spawn=None)
# Start the server greenlets
http_server.start()
# to start the server asynchronously, call server.start()
# we use blocking serve_forever() here because we have no other jobs
server.serve_forever()
I'm getting this exception where it gives me failed to handle request from werkzeug server.
I've used only werkzeug utility and have never used werkzeug server..
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gevent/server.py", line 131, in _do_accept
self._handle(client_socket, address)
File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 571, in handle
handler.handle()
File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 180, in handle
result = self.handle_one_request()
File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 287, in handle_one_request
raw_requestline = self.read_requestline()
File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 280, in read_requestline
return self.rfile.readline(MAX_REQUEST_LINE)
File "/usr/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
File "/usr/local/lib/python2.7/dist-packages/gevent/socket.py", line 432, in recv
wait_read(sock.fileno(), timeout=self.timeout, event=self._read_event)
File "/usr/local/lib/python2.7/dist-packages/gevent/socket.py", line 169, in wait_read
switch_result = get_hub().switch()
File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 154, in switch
assert cur is not self, 'Cannot switch to MAINLOOP from MAINLOOP'
AssertionError: Cannot switch to MAINLOOP from MAINLOOP
<WSGIServer fileno=4 address=0.0.0.0:4000>: Failed to handle request from ('127.0.0.1', 36088)

Resources