What is the correct way to use source_address - python

I have come across a corner in socket in Python3.5. When running the code below something is wrong and an error is thrown.
import socket
print(socket.create_connection(('www.baidu.com', 80), source_address=('127.0.0.1', 8998)))
Error:
Traceback (most recent call last):
File "demo.py", line 4, in <module>
print(socket.create_connection(('www.baidu.com', 80), source_address=('127.0.0.1', 8998)))
File "/usr/local/lib/python3.5/socket.py", line 707, in create_connection
raise err
File "/usr/local/lib/python3.5/socket.py", line 698, in create_connection
sock.connect(sa)
OSError: [Errno 22] Invalid argument

Short answer is don't!
You can't play with low level Python sockets not knowing how BSD TCP stack is working and how to use BSD sockets.
The source_address is - as it clearly states - source address and port for you connection. You MUST NOT make connection from loopback interface address to the address in the outside world.
So, the invalid argument error is correct.
Do you really need to set source_address? Why don't let that to the operating system?

You can't use the source address 127.0.0.1. The server can't reach you on this address, its just locally on your computer. This is your local loopback address. You have to use your public IP address.
So best is let the system set the source address like the other answer says.
socket.create_connection(('www.baidu.com', 80))

Related

Is there a way of setting TLS Version using the redshift-connector driver in Python?

We're currently using the redshift-connector driver in Python to connect to a a Redshift database.
This is working locally however when deploying to a server with limited internet connectivity we're seeing SSL handshake errors.
When observing this on Wireshark we see locally the handshake uses TLSv1.2, however on the server we see TLSv1 and no response which we believe is the culprit. Is there a way of specifying the TLS version on a connection made with the redshift-connector?
Code:
import redshift_connector
connection = redshift_connector.connect(
host='hostip',
database='db',
user=’username’,
password=’password’,
timeout=120
)
Error:
Traceback (most recent call last):
File "redshift.py", line 9, in <module>
timeout=20
File "C:\Python37\lib\site-packages\redshift_connector\__init__.py", line 334, in connect
credentials_provider=info.credentials_provider,
File "C:\Python37\lib\site-packages\redshift_connector\core.py", line 587, in __init__
raise InterfaceError("communication error", e)
redshift_connector.error.InterfaceError: ('communication error', timeout('_ssl.c:1074: The handshake operation timed out'))
We've verified the port is open via telnet so this isn't a firewall issue. Additionally we used ssl=false and can connect without any problems but would prefer this to be enabled
Many thanks for any help!

How to use asyncio create_connection for IPv6 addresses?

In my code I've tried using asyncio create_connection to connect to get clients to connect to a TCP server. For some reason, the connection is successful when I try use an IPv4 address:
event_loop.create_connection(client_factory, host='127.0.0.1', port=8190)
But the connection fails when I use an IPv6 address:
event_loop.create_connection(client_factory, host='::1', port=8190)
When I use the IPv6 loopback address, I get an OSError:
Traceback (most recent call last):
File "C:\Users\<removed>\AppData\Local\Programs\Python\Python36\lib\asyncio\base_events.py", line 778, in create_connection
raise exceptions[0]
File "C:\Users\<removed>\AppData\Local\Programs\Python\Python36\lib\asyncio\base_events.py", line 765, in create_connection
yield from self.sock_connect(sock, address)
File "C:\Users\<removed>\AppData\Local\Programs\Python\Python36\lib\asyncio\proactor_events.py", line 451, in sock_connect
return self._proactor.connect(sock, address)
File "C:\Users\<removed>\AppData\Local\Programs\Python\Python36\lib\asyncio\windows_events.py", line 511, in connect
ov.ConnectEx(conn.fileno(), address)
OSError: [WinError 10022] An invalid argument was supplied
Looking at the documentation, it looks like the family parameter for create_connection defaults to socket.AF_UNSPEC. I also tried the same call by specifying the family:
event_loop.create_connection(client_factory, host='::1', port=8190, family=socket.AF_INET6)
But I get the same result. I know setting host to 'localhost' will work for this example, but I want to know how to use any IPv6 address for create_connection.

Sending UDP requests through Tor (SOCKS5) with Python

So I have a python script that needs to send a packet to my server 'x.x.x.x'. I've been able to successfully initialise Tor through Python by setting up the SOCKS5 proxy, but upon trying to send a packet to my server I get the error:
Traceback (most recent call last):
File "test.py", line 18, in <module>
sock.sendto(bytes, ("x.x.x.x", 6000))
File "/usr/local/lib/python2.7/dist-packages/socks.py", line 338, in sendto
self.bind(("", 0))
File "/usr/local/lib/python2.7/dist-packages/socks.py", line 325, in bind
_, relay = self._SOCKS5_request(self._proxyconn, UDP_ASSOCIATE, dst)
File "/usr/local/lib/python2.7/dist-packages/socks.py", line 494, in _SOCKS5_request
raise SOCKS5Error("{0:#04x}: {1}".format(status, error))
socks.SOCKS5Error: 0x07: Command not supported, or protocol error
From what I've seen, socket/SOCKS5 doesn't support connectionless UDP, so I attempted connecting to the port and then sending the packet once connected. I still get the same error as seen above, output can be seen below.
Traceback (most recent call last):
File "test.py", line 18, in <module>
sock.connect(("x.x.x.x", 6000))
File "/usr/local/lib/python2.7/dist-packages/socks.py", line 698, in connect
self.bind(("", 0))
File "/usr/local/lib/python2.7/dist-packages/socks.py", line 325, in bind
_, relay = self._SOCKS5_request(self._proxyconn, UDP_ASSOCIATE, dst)
File "/usr/local/lib/python2.7/dist-packages/socks.py", line 494, in _SOCKS5_request
raise SOCKS5Error("{0:#04x}: {1}".format(status, error))
socks.SOCKS5Error: 0x07: Command not supported, or protocol error
Seeing as a UDP connection does not work either, I would prefer to stay connectionless as this makes my intended use simpler as the port is not necessarily active/open at any given time. The script I'm using to attempt to send the packet can be seen below. I've added but commented out the connectionless and connection methods I was using. Ignore all the extra imports at the top, these are for use later in the script development.
import socks
import socket
import requests
from TorCtl import TorCtl
import urllib2
import random
import math
import time
socks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_SOCKS5, addr="127.0.0.1", port=9050)
socket.socket = socks.socksocket
print requests.get("http://icanhazip.com").text
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
bytes=random._urandom(1024)
# UDP CONNECTION METHOD
#sock.connect(("x.x.x.x", 6000))
#sock.send(bytes)
# UDP CONNECTIONLESS METHOD
#sock.sendto(bytes, ("x.x.x.x", 6000))
Which brings me to my question - is there any way to send UDP packets via a connectionless method through a SOCKS5 proxy in python?
UPDATE
I originally had SocksiPy installed instead of PySocks, so I've replaced the modules and removed the monkeypatch in the original script. But now, I'm instead getting 'Broken Pipe' errors, as seen below.
Traceback (most recent call last):
File "test.py", line 19, in <module>
s.sendto(bytes, ("x.x.x.x", 6000))
File "/usr/local/lib/python2.7/dist-packages/socks.py", line 336, in sendto
return _BaseSocket.sendto(self, bytes, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/socks.py", line 223, in <lambda>
return lambda self, *pos, **kw: self._savedmethods[name](*pos, **kw)
socket.error: [Errno 32] Broken pipe
I've double checked to make sure Tor is actually working and the proxy is up, which it is as I can receive HTTP requests and responses through the Tor proxy - the responses to sites like http://icanhazip.com/ return a different IP that my actual IP, which suggests that Tor is indeed functioning. I also figured that removing the monkeypatch might make it work with better compatibility, seeing as with the new PySocks module on the OLD script it still fails with the original errors seen above.
The new script:
import socks
import socket
import random
import math
s = socks.socksocket()
s.set_proxy(socks.SOCKS5, "localhost", 9050)
bytes=random._urandom(1024)
# UDP CONNECTION METHOD
#sock.connect(("x.x.x.x", 6000))
#sock.send(bytes)
# UDP CONNECTIONLESS METHOD
#s.sendto(bytes, ("x.x.x.x", 6000))
The errors I'm describing above are with the connectionless method - using the connection method seems to possibly work, however it hangs when connecting to the port (which is the be expected, as the port isn't open).
As #gwyn pointed out, Tor only supports TCP streams, as specified on their website. Using UDP over Tor will not work, only TCP connections.

Flask, cannot assign requested address [duplicate]

This question already has answers here:
socket.error:[errno 99] cannot assign requested address and namespace in python
(6 answers)
Closed 8 years ago.
I'm trying to run a flask app on a remote server, so I can access it from other computers. The server has a public IP and I configured the flask to run on that IP. But when I run the script I get the following traceback
Note: I've removed the public IP from the traceback and my code.
* Running on **public ip**
Traceback (most recent call last):
File "testServer.py", line 14, in <module>
app.run(host='62.60.19.189',port=5000)
File "/usr/lib/python2.6/site-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 710, in run_simple
inner()
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 692, in inner
passthrough_errors, ssl_context).serve_forever()
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 486, in make_server
passthrough_errors, ssl_context)
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 410, in __init__
HTTPServer.__init__(self, (host, int(port)), handler)
File "/usr/lib64/python2.6/SocketServer.py", line 402, in __init__
self.server_bind()
File "/usr/lib64/python2.6/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/usr/lib64/python2.6/SocketServer.py", line 413, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
socket.error: [Errno 99] Cannot assign requested address
Here is my code
import flask
app = flask.Flask("My app")
#app.route('/myroute', methods=['POST'])
def foobar():
print flask.request.form
return '<br>'.join('{0}: {1}'.format(*pair) for pair in flask.request.form.items())
if __name__ == '__main__':
app.run(host='public IP',port=5000)
You can only directly bind to an IP address that the server has been configured for; behind a router running Network Address Translation (NAT) your internal IP address will be different.
Either bind directly to that internal IP address, or use '0.0.0.0' to listen on all interfaces. You may still need to configure the router to forward a specific port to your internal server.
The IP you want to bind a socket to must be directly available on an interface of the machine. This seems not be the case here.
If you're behind a NAT: use port-forwarding
If you're using a VPN and the VPN adapter of the server is not always up, try using "0.0.0.0" as an address. Beware: It will listen on all interfaces available. Create firewall rules to block access via interfaces you don't want to listen to when using this.

What is causing paramiko.SSHException: Invalid packet blocking?

When I attempt to connect to one of our internal servers using paramiko (inside of fabric, for what it's worth) I get this error:
Retrieving packages from server p-websvr-004
[p-websvr-004] run: /usr/sbin/pkg_info -aD|grep "Information for"
starting thread (client mode): 0x179f090L
Banner: ----------------------------------------------------------------------
Banner: Welcome to Mycompany, Inc. Unauthorized access, is strictly prohibited
Connected (version 2.0, client OpenSSH_4.5p1)
Exception: Invalid packet blocking
Traceback (most recent call last):
File "/Users/crose/virtualenv/mycompany/lib/python2.6/site-packages/paramiko/transport.py", line 1491, in run
ptype, m = self.packetizer.read_message()
File "/Users/crose/virtualenv/mycompany/lib/python2.6/site-packages/paramiko/packet.py", line 344, in read_message
raise SSHException('Invalid packet blocking')
Every other host we have works, as far as I can tell. What's causing this to happen, and how can I fix it?
First obvious question, how is this host different than the rest?
By the looks of it, it could be a bug in the SSH server. Does openssh on the command line work, and is it using a different cipher?

Categories

Resources