Skip to content

Instantly share code, notes, and snippets.

@nickmbailey
Created November 11, 2015 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickmbailey/b047390a5effa1b20856 to your computer and use it in GitHub Desktop.
Save nickmbailey/b047390a5effa1b20856 to your computer and use it in GitHub Desktop.
Debugging jython issue.
"""
socket_workout is a short script designed to reproduce a socket behavior
difference between Jython and CPython.
"""
import socket
import errno
import threading
import traceback
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('socket_workout')
class SocketConnectThread(threading.Thread):
def __init__(self, address, name):
threading.Thread.__init__(self, name=name)
self.address = address
self.connect_status_results = []
self.final_result = "Test not finished"
def run(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
try:
connect_errno = 0
connect_attempt = 0
while connect_errno != errno.EISCONN and connect_attempt < 100:
connect_attempt += 1
connect_errno = sock.connect_ex(self.address)
self.connect_status_results.append(str(connect_errno))
if connect_errno == errno.EISCONN:
self.final_result = "[%s] socket connected - attempts: %s, results = [%s]" % \
(self.getName(), str(connect_attempt), ",".join(self.connect_status_results))
else:
self.final_result = "[%s] socket failed to connect - attempts: %s, results = [%s]" % \
(self.getName(), str(connect_attempt), ",".join(self.connect_status_results))
except:
# this shouldn't happen
self.final_result = traceback.format_exc()
finally:
try:
sock.shutdown(0)
sock.shutdown(1)
sock.close()
except:
pass
if __name__ == "__main__":
connect_threads = [SocketConnectThread(address=('127.0.0.1', 8000),
name="SocketTest-" + str(i)) for i in xrange(5)]
for thread in connect_threads:
thread.start()
for thread in connect_threads:
thread.join()
logger.debug(thread.final_result)
@nickmbailey
Copy link
Author

[Nicks-MBP:13:07:57 master*] Downloads$ python -m SimpleHTTPServer 8000 &
[1] 4379

[Nicks-MBP:13:08:05 master*] Downloads$ python socket_workout.py
DEBUG:socket_workout:[SocketTest-0] socket connected - attempts: 6, results = [36,37,37,37,37,56]
DEBUG:socket_workout:[SocketTest-1] socket connected - attempts: 3, results = [36,37,56]
DEBUG:socket_workout:[SocketTest-2] socket connected - attempts: 23, results = [36,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,56]
DEBUG:socket_workout:[SocketTest-3] socket connected - attempts: 2, results = [36,56]
DEBUG:socket_workout:[SocketTest-4] socket connected - attempts: 2, results = [36,56]

[Nicks-MBP:13:08:21 master*] Downloads$ java -jar jython-standalone-2.5.4-rc1.jar socket_workout.py
DEBUG:socket_workout:[SocketTest-0] socket connected - attempts: 1, results = [56]
DEBUG:socket_workout:[SocketTest-1] socket connected - attempts: 1, results = [56]
DEBUG:socket_workout:[SocketTest-2] socket connected - attempts: 1, results = [56]
DEBUG:socket_workout:[SocketTest-3] socket connected - attempts: 1, results = [56]
DEBUG:socket_workout:[SocketTest-4] socket connected - attempts: 1, results = [56]

[Nicks-MBP:13:09:04 master*] Downloads$ java -jar jython-standalone-2.7.1b1.jar socket_workout.py
DEBUG:socket_workout:[SocketTest-0] socket connected - attempts: 35, results = [36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,56]
DEBUG:socket_workout:[SocketTest-1] socket failed to connect - attempts: 100, results = [36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36]
DEBUG:socket_workout:[SocketTest-2] socket connected - attempts: 94, results = [36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,56]
DEBUG:socket_workout:[SocketTest-3] socket failed to connect - attempts: 100, results = [36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57]
DEBUG:socket_workout:[SocketTest-4] socket failed to connect - attempts: 100, results = [36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment