Nasty

Jython server socket example

Aug 28th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. from java.lang import Thread
  2. from java.io import IOException, DataOutputStream, DataInputStream
  3. from java.net import Socket, ServerSocket
  4. from java.lang import String
  5.  
  6.  
  7. class Handler(Thread):
  8.     def __init__(self, connection):
  9.         try:
  10.             self.data_in = DataInputStream(connection.getInputStream())
  11.             self.data_out = DataOutputStream(connection.getOutputStream())
  12.             print 'new connection'
  13.         except IOException, e:
  14.             e.printStackTrace()
  15.            
  16.     def run(self):
  17.         self.send('hi')
  18.        
  19.     def send(self, data):
  20.         try:
  21.             self.data_out.write(String.getBytes(data))
  22.             self.data_out.flush()
  23.         except IOException, e:
  24.             e.printStackTrace()
  25.  
  26. listen = ServerSocket(1234)
  27. print 'server ready'
  28. while True:
  29.     conn = listen.accept()
  30.     handler = Handler(conn)
  31.     handler.start()
Advertisement
Add Comment
Please, Sign In to add comment