Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

TcpHandle: TCP connection port parse error #32

Open
thica opened this issue Dec 6, 2016 · 6 comments
Open

TcpHandle: TCP connection port parse error #32

thica opened this issue Dec 6, 2016 · 6 comments

Comments

@thica
Copy link

thica commented Dec 6, 2016

In common.py, you split (in init of TcpHandle) the given serial (which in effect is host:port). port results as a string, but socket expects port to be numeric in socket.create_connection

Python 2.7

@fahhem
Copy link
Contributor

fahhem commented Dec 7, 2016

Hmm, TcpHandle was a contributed class which I know the contributor uses, so it's odd that it would have such a blatant bug. If you're able to test your changes, then a pull request would be accepted.

@thica
Copy link
Author

thica commented Dec 7, 2016

Hi,

its tested

`class TcpHandle(object):
"""TCP connection object.

 Provides same interface as UsbHandle but ignores timeout."""

def init(self, serial):
"""Initialize the TCP Handle.
Arguments:
serial: Android device serial of the form host or host:port.

Host may be an IP address or a host name.
"""
if ':' in serial:
  (host, port) = serial.split(':')
  port_int=int(port)  # CTH Code adjusted
else:
  host = serial
  port = 5555
self._serial_number = '%s:%s' % (host, port)

self._connection = socket.create_connection((host, port_int))  # CTH Code adjusted
`

You can easy tell, that the line
port = 5555
indicates a numeric value.

@thica
Copy link
Author

thica commented Dec 7, 2016

Sorry just seen, that the default path have to covered as well

`
class TcpHandle(object):
"""TCP connection object.

 Provides same interface as UsbHandle but ignores timeout."""

def init(self, serial):
"""Initialize the TCP Handle.
Arguments:
serial: Android device serial of the form host or host:port.

Host may be an IP address or a host name.
"""
if ':' in serial:
  (host, port) = serial.split(':')
  port_int=int(port)  # CTH Code adjusted
else:
  host = serial
  port = '5555' # CTH Code adjusted
  port_int = 5555 # CTH Code adjusted
self._serial_number = '%s:%s' % (host, port)

self._connection = socket.create_connection((host, port_int)) # CTH Code adjusted

`

@ghost
Copy link

ghost commented Jan 27, 2017

Hi @thica !

In fact socket.create_connection can take a tuple of (str, str) or (str, int).

>>> socket.create_connection(('127.0.0.1', '12800'))
<socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 39774), raddr=('127.0.0.1', 12800)>
>>> socket.create_connection(('127.0.0.1', 12800))
<socket.socket fd=5, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 39778), raddr=('127.0.0.1', 12800)>

So we do not need to cast port value. 😃

@thica
Copy link
Author

thica commented Jan 28, 2017

Hi checked it out, you are right. I am running on an error, as all my strings in my app are unicode, which can't be handled by socket.
socket.create_connection(('127.0.0.1', u'8080'))
Traceback (most recent call last):
File "", line 1, in
File "D:\dev\Kivy27\Python27\lib\socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.error: getaddrinfo() argument 2 must be integer or string

@fahhem
Copy link
Contributor

fahhem commented Jan 28, 2017

You'll need to encode the port as bytes before passing it to TcpHandle then: port.encode('ascii')

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants