Python, TOR, Requests - 'module' object has no attribute 'inet_pton' - python

I am trying to make a GET request over TOR which is listening on 127.0.0.1:9050
I've installed request socks: pip install -U requests[socks]
import requests
tor_proxy = {'http': 'socks5://127.0.0.1:9050', 'https': 'socks5://127.0.0.1:9050'}
r = requests.get("http://www.google.com", proxies=tor_proxy, timeout=20)
Here is the error from when I run the code
Traceback (most recent call last):
r = requests.get("http://www.google.com", proxies=tor_proxy, timeout=20)
File "C:\Python27\lib\site-packages\requests\api.py", line 71, in get
return request('get', url, params=params, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 57, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 585, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 403, in send
timeout=timeout
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 578, in urlopen
chunked=chunked)
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 362, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Python27\lib\httplib.py", line 1057, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 1097, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 1053, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 897, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 859, in send
self.connect()
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connection.py", line 167, in connect
conn = self._new_conn()
File "C:\Python27\lib\site-packages\requests\packages\urllib3\contrib\socks.py", line 81, in _new_conn
**extra_kw
File "C:\Python27\lib\site-packages\socks.py", line 195, in create_connection
sock.connect((remote_host, remote_port))
File "C:\Python27\lib\site-packages\socks.py", line 747, in connect
negotiate(self, dest_addr, dest_port)
File "C:\Python27\lib\site-packages\socks.py", line 419, in _negotiate_SOCKS5
CONNECT, dest_addr)
File "C:\Python27\lib\site-packages\socks.py", line 482, in _SOCKS5_request
resolved = self._write_SOCKS5_address(dst, writer)
File "C:\Python27\lib\site-packages\socks.py", line 517, in _write_SOCKS5_address
addr_bytes = socket.inet_pton(family, host)
AttributeError: 'module' object has no attribute 'inet_pton'
Jun 11 13:13:55.000 [notice] Tried for 120 seconds to get a connection to [scrubbed]:0. Giving up. (waiting for socks info)
Thanks

The problem is with the library itself, in particular PySocks socks.py, inet_pton is only available on unix:
socket.inet_pton(address_family, ip_string)
Convert an IP address from its family-specific string format to a packed, binary format. inet_pton() is useful when a library or network protocol calls for an object of type struct in_addr (similar to inet_aton()) or struct in6_addr.
Supported values for address_family are currently AF_INET and AF_INET6. If the IP address string ip_string is invalid, socket.error will be raised. Note that exactly what is valid depends on both the value of address_family and the underlying implementation of inet_pton().
Availability: Unix (maybe not all platforms).
One workaround is to install win_inet_pton and import it in your script, the method will automatically be added to the socket lib.

Related

error uploading file to google drive with python

I wrote a code to upload (create and update) a file to google drive,
in Windows 10 with python 3.9 it work, but in windows 2008 server with python 3.8 it give me an error.
just to remember 3.8 is the max version that supports windows 2008
if I try to list from gdrive it work, the problem is just to create or update the file.
just to remember 3.8 is the last python version that supports windows 2008.
I suspect its related with windows 2008 and ssl maybe!?!?
the error is this:
C:\backupmgr>python drive.py
Traceback (most recent call last):
File "drive.py", line 112, in <module>
envia_zip('sexta.7z')
File "drive.py", line 104, in envia_zip
file = service.files().create(body=file_metadata, media_body=media).execute(
)
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\googleapiclient\http.py", line 923, in execute
resp, content = _retry_request(
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\googleapiclient\http.py", line 222, in _retry_request
raise exception
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\googleapiclient\http.py", line 191, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\google_auth_httplib2.py", line 218, in request
response, content = self.http.request(
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\httplib2\__init__.py", line 1720, in request
(response, content) = self._request(
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\httplib2\__init__.py", line 1440, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, he
aders)
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\httplib2\__init__.py", line 1363, in _conn_request
conn.request(method, request_uri, body, headers)
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\http\c
lient.py", line 1252, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\http\c
lient.py", line 1298, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\http\c
lient.py", line 1247, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\http\c
lient.py", line 1046, in _send_output
self.send(chunk)
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\http\c
lient.py", line 968, in send
self.sock.sendall(data)
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\ssl.py
", line 1204, in sendall
v = self.send(byte_view[count:])
File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\ssl.py
", line 1173, in send
return self._sslobj.write(data)
socket.timeout: The write operation timed out
Well it works now, as #DaImTo poited to the issue #632 in the google api github, it is not a problem with the api. The problem is that the socket core module has low default timeout. The pc with windows server 2008 that I was using was very slow and was hiting this default timeout, so I just had rise the default timeout by inserting the code in the beginin of the script:
import socket
socket.setdefaulttimeout(600)

Having problems with python requests

I can't request any page. Am I doing something wrong? I've used this code before, so I don't know what's happening. I've tried other ways as well. Seems to just not get a response. Did something change with requests? Or could it be an anti-virus firewall thing?
Any help?
>>> import requests, json
>>> url = 'https://api.chess.com/pub/player/Hikaru/games/archives'
>>> resp= requests.get(url)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
resp= requests.get(url)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1016, in _send_output
self.send(msg)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 977, in send
self.sock.sendall(data)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\contrib\pyopenssl.py", line 328, in sendall
sent = self._send_until_done(data[total_sent:total_sent + SSL_WRITE_BLOCKSIZE])
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\contrib\pyopenssl.py", line 317, in _send_until_done
return self.connection.send(data)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\site-packages\OpenSSL\SSL.py", line 1644, in send
with _from_buffer(buf) as data:
AttributeError: __enter__
import requests, json
url = 'https://api.chess.com/pub/player/Hikaru/games/archives'
resp= requests.get(url)
print(resp)

Tor and Python3 using SocksiPy

I copy pasted a code from another post which for them, seems fine but mine prints an error and I don't know why... How to make urllib2 requests through Tor in Python?
Anyone can help me with this error?
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9150)
socket.socket = socks.socksocket
import urllib.request
print(urllib.request.urlopen('http://www.google.com').read())
Traceback (most recent call last):
File "/Users/Raphael/Desktop/whatsmyip.py", line 17, in <module>
r = requests.get("http://checkip.dyn.com")
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/api.py", line 68, in get
return request('get', url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/adapters.py", line 370, in send
timeout=timeout
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 349, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1088, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1126, in _send_request
self.endheaders(body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1084, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 922, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 857, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 155, in connect
conn = self._new_conn()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 134, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 78, in create_connection
sock.connect(sa)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socks.py", line 369, in connect
self.__negotiatesocks5(destpair[0],destpair[1])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socks.py", line 176, in __negotiatesocks5
self.sendall("\x05\x01\x00")
TypeError: 'str' does not support the buffer interface
and do anyone know why? I would really like to use this module but it doesn't work!

HTTP request with Python : TypeError: an integer is required (got type socket)

I'm facing a really strange bug while trying to retrieve a resource via HTTP. It seems to happen with any HTTP client (tried requests and urllib with the same results).
My project uses django, and I run my tests using tox and the standard django command python manage.py test. When I run my test suite and a unit test makes a HTTP request (e.g via requests.get('http://example.com')), the tests fails with an error, the test suite continue until the end, and hangs. I have to manually kill the process via command line.
After some investigations, I put a try / except block arount the http request, and got the following stacktrace:
File "/mycomputer/python3.4/site-packages/requests/api.py", line 68, in get
return request('get', url, **kwargs)
File "/mycomputer/python3.4/site-packages/requests/api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "/mycomputer/python3.4/site-packages/requests/sessions.py", line 464, in request
resp = self.send(prep, **send_kwargs)
File "/mycomputer/python3.4/site-packages/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "/mycomputer/python3.4/site-packages/requests/adapters.py", line 370, in send
timeout=timeout
File "/mycomputer/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "/mycomputer/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 349, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.4/http/client.py", line 1065, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.4/http/client.py", line 1103, in _send_request
self.endheaders(body)
File "/usr/lib/python3.4/http/client.py", line 1061, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.4/http/client.py", line 906, in _send_output
self.send(msg)
File "/usr/lib/python3.4/http/client.py", line 841, in send
self.connect()
File "/mycomputer/python3.4/site-packages/requests/packages/urllib3/connection.py", line 155, in connect
conn = self._new_conn()
File "/mycomputer/python3.4/site-packages/requests/packages/urllib3/connection.py", line 134, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/mycomputer/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 68, in create_connection
sock = socket.socket(af, socktype, proto)
File "/usr/lib/python3.4/socket.py", line 123, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
TypeError: an integer is required (got type socket)
I really don't understand the problem here. Running the same thing from the command line works perfectly, so it's probably related to my project architecture. Also, running the test suite on another computer fails the same way.
Have anybody meet a similar issue ? What can I do do track down the problem ?
Okay, the problem was caused by HTTPretty, a third-party package I use for mocking.
Somewhere I've read that django is not a full server and can cause problems when you try to use it as one. Perhaps this is one of this cases.
Did you try https://docs.djangoproject.com/en/1.8/topics/testing/advanced/ ?

TypeError: expected a readable buffer object

I am running into the following error when I try to connect to an API using M2Crypto HTTPSConnection library using the GET method.
Traceback (most recent call last):
File "osg-gridadmin-cert-request", line 665, in <module>
status = check_quota_limit(request_count)
File "osg-gridadmin-cert-request", line 543, in check_quota_limit
conn.request('GET', arguments['quotaurl'], headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 941, in request
self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 975, in _send_request
self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 937, in endheaders
self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 801, in _send_output
self.send(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 773, in send
self.sock.sendall(data)
File "build/bdist.macosx-10.7-intel/egg/M2Crypto/SSL/Connection.py", line 217, in write
return self._write_nbio(data)
File "build/bdist.macosx-10.7-intel/egg/M2Crypto/SSL/Connection.py", line 202, in _write_nbio
return m2.ssl_write_nbio(self.ssl, data)
TypeError: expected a readable buffer object
I am accessing the API using this code
conn = M2Crypto.httpslib.HTTPSConnection(arguments['host'],
ssl_context=ssl_context)
conn.request('GET', arguments['quotaurl'], headers)
response = conn.getresponse()
Could you please help me with this problem?
See the error message
self._send_request(method, url, body, headers)
This indicates that you are passing "headers" where you are supposed to pass "body".
So, try calling
conn.request('GET', arguments['quotaurl'], None, headers)

Categories

Resources