captmicro

Untitled

Oct 10th, 2011
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import socket
  2.  
  3. #INITIAL CONTACT SERVER
  4. ics_addr = ('', 4760) #any interface, port 4763
  5. ics = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #tcp socket
  6. ics.bind(ics_addr) #listen on this address
  7. ics.listen(5) #backlog, dont mind this
  8.  
  9. req2srv = {
  10. '1': '127.0.0.1:4761', #type 1
  11. '2': '127.0.0.1:4762', #type 2
  12. '3': '127.0.0.1:4763', #type 3
  13. }
  14.  
  15. listen = True
  16. while listen: #accept connections until listen==False
  17. client, addr = srv.accept() #wait for client to connect
  18. data = client.recv(1024) #recv 1024 bytes (max, can be less) of data
  19. if data: #if data==False recv failed
  20. req_type = data.strip() #strip whitespace from both ends
  21. srv = req2srv(req_type) #get server address from request type
  22. client.send(srv) #send client server address
  23. client.close() #close client socket
  24. ics.close() #close ics socket
  25.  
  26.  
Add Comment
Please, Sign In to add comment