Timeout Error when using proxy with httplib2 in Python - python

i have a simple code that uses proxy to submit a "GET" request to google .
import httplib2
http = httplib2.Http(proxy_info = httplib2.ProxyInfo(proxy_type=3,proxy_host=myProxy, proxy_port=myPort))
resp, content = http.request("http://google.com", "GET")
print(resp)
print(content)
For some reason i get a Timeout error :
resp, content = http.request("http://google.com", "GET")
File "C:\Python35\lib\site-packages\httplib2\__init__.py", line 1322, in requ
st
(response, content) = self._request(conn, authority, uri, request_uri, meth
d, body, headers, redirections, cachekey)
File "C:\Python35\lib\site-packages\httplib2\__init__.py", line 1072, in _req
est
(response, content) = self._conn_request(conn, request_uri, method, body, h
aders)
File "C:\Python35\lib\site-packages\httplib2\__init__.py", line 995, in _conn
request
conn.connect()
File "C:\Python35\lib\http\client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Python35\lib\socket.py", line 711, in create_connection
raise err
File "C:\Python35\lib\socket.py", line 702, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connecte
party did not properly respond after a period of time, or established connecti
n failed because connected host has failed to respond
I'm using a valid proxy, but this module doesn't works, Does anybody knows why this happened?

Win Error 10060 means that no connection can be made and that either the host or the connection(proxy) is at fault. Since google is virtually never down we can narrow down the problem to the proxy configuration.
Since you seemingly configured the proxy correctly (assuming you pass the right types and arugments ) you might want to check what SOCKS protocol the server additionally supports.
Either your're not passing valid values as a proxy configuration or it is the server's fault.
You could also try to diagnose with wireshark to see if any packets even make it to the server.

Related

Sending multiple sms in quick succession using Twilio

I am trying to send sms using twilio and django 1.8.2. I am facing a problem when there are multiple calls to send sms within the same function.
This is the code snippet to send an sms to any particular phone number:
def send_twilio_message(to_number, body):
client = twilio.rest.TwilioRestClient(
settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
return client.messages.create(
body=body,
to=to_number,
from_=settings.TWILIO_PHONE_NUMBER
)
When I make two requests to this function like this:
def some_function():
send_twilio_message(number1, "text 1")
send_twilio_message(number2, "text 2")
I am getting this error:
Traceback:
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "/home/arka/Project/TreadWill/dreamhost/CCBT/trial_IITK/views.py"
in waitlist_phq
782. exclusion_code = waitlist_alert(wl.participant,
phq_score)
File "/home/arka/Project/TreadWill/dreamhost/CCBT/trial_IITK/views.py"
in waitlist_alert
857. send_twilio_message(phonenumber, 'Looks like your
child maybe having suicidal thoughts. Get in touch with your child
ASAP. - TreadWill.')
File "/home/arka/Project/TreadWill/dreamhost/CCBT/sendsms/utils.py" in
send_twilio_message
13. from_=settings.TWILIO_PHONE_NUMBER
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/messages.py" in create
122. return self.create_instance(kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in create_instance
365. data=transform_params(body))
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in request
200. resp = make_twilio_request(method, uri, auth=self.auth,
**kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in make_twilio_request
152. resp = make_request(method, uri, **kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in make_request
117. resp, content = http.request(url, method, headers=headers,
body=data)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in request
1314. (response, content) = self._request(conn, authority, uri,
request_uri, method, body, headers, redirections, cachekey)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in _request
1064. (response, content) = self._conn_request(conn, request_uri,
method, body, headers)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in _conn_request
987. conn.connect()
File "/usr/lib/python3.5/http/client.py" in connect
1260. server_hostname=server_hostname)
File "/usr/lib/python3.5/ssl.py" in wrap_socket
377. _context=self)
File "/usr/lib/python3.5/ssl.py" in __init__
752. self.do_handshake()
File "/usr/lib/python3.5/ssl.py" in do_handshake
988. self._sslobj.do_handshake()
File "/usr/lib/python3.5/ssl.py" in do_handshake
633. self._sslobj.do_handshake()
Exception Type: SSLError at
/iitk/phq/NnBLRUEsbm5jbERmfGZWNkt6RVNPcEFBNnplNkA4RVFOTkN4TnI=/
Exception Value: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify
failed (_ssl.c:645)
But, when I do this:
import time
def some_function():
send_twilio_message(number1, "text 1")
time.sleep(60)
send_twilio_message(number2, "text 2")
it is working fine. The problem with this approach is that the user might close the browser if the page keeps loading for 60 seconds (I've tried for less than 60 seconds, I get the same error).
I've tried disabling SSL verification in my Twilio account, but that didn't work.
I've looked into other answers, and I've gone through Twilio's documentation. I haven't been able to figure out a good solution. Any help will be appreciated.
Edit:
I was trying all this on my localserver. When I tried the code:
def some_function():
send_twilio_message(number1, "text 1")
send_twilio_message(number2, "text 2")
on a hosted server, it worked fine. So, I think the problem might be due to my institute's network. The problem is resolved for me, but I still haven't been able to figure out why I was facing the issue. If anyone has any idea why this might be happening, please post your answer.

Python httplib2 "httplib2.SSLHandshakeError"

I have this piece of code that tries to get the page content from a given url.
import httplib2
start_url = "https://www.somesite.com"
http = httplib2.Http(disable_ssl_certificate_validation=True)
status, response = http.request(start_url)
However, when I run it, I get this error:
Traceback (most recent call last): File "C:\Documents and Settings\DD\Desktop\crawler.py", line 15, in <module>
resp, content = h.request(start_url, "GET") File "C:\Python27\lib\site-packages\httplib2-0.9-py2.7.egg\httplib2\__init__.py", line 1593, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey) File "C:\Python27\lib\site-packages\httplib2-0.9-py2.7.egg\httplib2\__init__.py", line 1335, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers) File "C:\Python27\lib\site-packages\httplib2-0.9-py2.7.egg\httplib2\__init__.py", line 1257, in _conn_request
conn.connect() File "C:\Python27\lib\site-packages\httplib2-0.9-py2.7.egg\httplib2\__init__.py", line 1044, in connect
raise SSLHandshakeError(e) httplib2.SSLHandshakeError: [Errno 1] _ssl.c:510: error:1408F119:SSL routines:SSL3_GET_RECORD:decryption failed or bad record mac
As you may have seen, I tried to disable the ssl validation but with no succsess.
Any Help?
Thanks!
SSL3_GET_RECORD:decryption failed or bad record mac
This has nothing to do with SSL validation. It might be that the server simply does not talk SSL or that there are other SSL related problems, but validation is not one of them at this stage of communication.
If you provide the real URL or a full packet capture (file or at cloudshark.org) one might analyze the information in more detail.

httplib python error

I'm trying to send an http get request via the httplib, but I'm facing issues.
conn = httplib.HTTPConnection("10.30.111.13/View")
conn.request("GET", "/Default.aspx")
res = conn.getresponse()
if res.status == 200:
print(res.status)
else:
print("Something went terribly wrong")
I get the following error:
TypeError (cannot concatenate 'str' and 'int' objects).
If put the next line of codes, it works no problem:
conn = httplib.HTTPConnection("www.google.com")
conn.request("GET", "/")
EDIT, here is a more detailed log I managed to pull out of my third party software (it restricts me in turn of python usability):
File "<string>", line 3248, in initialization
File "C:\python22\lib\httplib.py", line 701, in request
self._send_request(method, url, body, headers)
File "C:\python22\lib\httplib.py", line 723, in _send_request
self.endheaders()
File "C:\python22\lib\httplib.py", line 695, in endheaders
self._send_output()
File "C:\python22\lib\httplib.py", line 581, in _send_output
self.send(msg)
File "C:\python22\lib\httplib.py", line 548, in send
self.connect()
File "C:\python22\lib\httplib.py", line 516, in connect
socket.SOCK_STREAM):
gaierror: (7, 'getaddrinfo failed')
I'm not someplace where I can test this now, but here's what I think:
You're passing only an IP address to a host field that's expecting a DNS address, not an IP address. That's why your second error listing says 'getaddrinfo' failed.
That said, I'm not sure how to use an IP address with httplib. Maybe try "http://10.30.111.13" instead. A good way to test it would be to replace your IP address above with Google's and see if you still get the error.
Maybe this will help -- sorry I can't say more!
I have changed the IP address for a DNS address. I also removed any path/URI that were in the HTTPConnection() parameter. Now it works. Sorry for such an obvious question guys.

Python YQL package Error

Hi I am new to Python...
I am trying to use YQL using Python.
I installed httplib2-0.7.0, oauth2 and then installed yql package
For this sample code :
import yql
y = yql.Public()
query = 'select * from flickr.photos.search where text="panda" limit 3';
result = y.execute(query)
print result
I got the following error message.
Please help!!
Traceback (most recent call last):
File "test.py", line 4, in
result = y.execute(query)
File "C:\Python27\lib\site-packages\yql-0.7-py2.7.egg\yql__init__.py", line 306, in execute
resp, content = self.http.request(url, http_method)
File "C:\Python27\lib\site-packages\httplib2__init__.py", line 1436, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey
)
File "C:\Python27\lib\site-packages\httplib2__init__.py", line 1188, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "C:\Python27\lib\site-packages\httplib2__init__.py", line 1123, in _conn_request
conn.connect()
File "C:\Python27\lib\site-packages\httplib2__init__.py", line 890, in connect
self.disable_ssl_certificate_validation, self.ca_certs)
File "C:\Python27\lib\site-packages\httplib2__init__.py", line 76, in _ssl_wrap_socket
cert_reqs=cert_reqs, ca_certs=ca_certs)
File "C:\Python27\lib\ssl.py", line 344, in wrap_socket
ciphers=ciphers)
File "C:\Python27\lib\ssl.py", line 119, in init
ciphers)
ssl.SSLError: [Errno 185090050] _ssl.c:336: error:0B084002:x509 certificate routines:X509_load_cert_crl_file:system lib
When I tried to use the *twitter python wrapper, I ended up getting the same SSL error.*
Please tell me what to do
I can see two likely issues:
SSL Certificate
I'm not familiar with Python or that library you're using, but the error sounds like it can't verify the SSL certificate. (Possibly because there is no suitable local SSL certificate bundle for authentication.) You may be able to configure it to skip the SSL certificate verification.
YQL Flickr Query
The YQL query is not correct and gives an error in the YQL console:
select * from flickr.photos.search where text="panda" limit 3
Actually, given the age of this question, it may have worked in June 2011. Now the Flickr tables require an API key as well, so the working query would look like:
select * from flickr.photos.search where text="panda" and api_key="insert-your-key-here" limit 3

python httplib Name or service not known

I'm trying to use httplib to send credit card information to authorize.net. When i try to post the request, I get the following traceback:
File "./lib/cgi_app.py", line 139, in run res = method()
File "/var/www/html/index.py", line 113, in ProcessRegistration conn.request("POST", "/gateway/transact.dll", mystring, headers)
File "/usr/local/lib/python2.7/httplib.py", line 946, in request self._send_request(method, url, body, headers)
File "/usr/local/lib/python2.7/httplib.py", line 987, in _send_request self.endheaders(body)
File "/usr/local/lib/python2.7/httplib.py", line 940, in endheaders self._send_output(message_body)
File "/usr/local/lib/python2.7/httplib.py", line 803, in _send_output self.send(msg)
File "/usr/local/lib/python2.7/httplib.py", line 755, in send self.connect()
File "/usr/local/lib/python2.7/httplib.py", line 1152, in connect self.timeout, self.source_address)
File "/usr/local/lib/python2.7/socket.py", line 567, in create_connection raise error, msg
gaierror: [Errno -2] Name or service not known
I build my request like so:
mystring = urllib.urlencode(cardHash)
headers = {"Content-Type": "text/xml", "Content-Length": str(len(mystring))}
conn = httplib.HTTPSConnection("secure.authorize.net:443", source_address=("myurl.com", 443))
conn.request("POST", "/gateway/transact.dll", mystring, headers)
to add another layer to this, it was working on our development server which has httplib 2.6 and without the source_address parameter in httplib.HTTPSConnection.
Any help is greatly appreciated.
===========================================================
EDIT:
I can run it from command line. Apparently this is some sort of permissions issue. Any ideas what permissions I would need to grant to which users to make this happen? Possibly Apache can't open the port?
As an (obvious) heads up, this same error can also be triggered by including the protocol in the host parameter. For example this code:
conn = httplib.HTTPConnection("http://secure.authorize.net", 80, ....)
will also cause the "gaierror: [Errno -2] Name or service not known" error, even if all your networking setup is correct.
gaierror: [Errno -2] Name or service not known
This error often indicates a failure of your DNS resolver. Does ping secure.authorize.net return successful replies from the same server that receives the gaierror? Does the hostname have a typo in it?
The problem ultimately came down to the fact that selinux was stopping apache from getting that port. Disabling selinux fixed the problems. I had an issue later where i didn't have /var/www/.python-eggs/, so MySQLdb was hosing on import. But after a mkdir, it was fixed.
pass the port separately from the host:
conn = httplib.HTTPSConnection("secure.authorize.net", 443, ....)

Categories

Resources