943,925 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 595
  • Python RSS
May 26th, 2009
0

Socket programming with a service

Expand Post »
I have written a program that is supposed to take a string via a socket connection and echo it back. This program is also run as a service. I can get a client program to connect to the service if the client program is on the same machine as the service, but if I try to connect from another machine, the client program will not connect. Does anyone have any idea about what might be wrong and how to fix it?

Thanks in advance
Similar Threads
Reputation Points: 15
Solved Threads: 0
Newbie Poster
mg0959 is offline Offline
18 posts
since Jan 2008
May 26th, 2009
0

Re: Socket programming with a service

Have you checked the firewall?
Try open the server port higher (>5000).

Check out if minimal implementations are working.
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
May 26th, 2009
0

Re: Socket programming with a service

Most likely issue is a firewall.

After that there's routers (port forwarding stuff) and proxies. Are you trying to connect over the internet, or on the same subnet?
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
May 26th, 2009
0

Re: Socket programming with a service

You can debug this yourself. Try pinging that ip address and port.

Helps to find root cause first
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
May 27th, 2009
0

Re: Socket programming with a service

Sorry to be so vague... I will supply the code to clarify.

I don't think there is a problem with firewalls. The program will work if I do not run it as a service, and the error I get on the client side is connection refused.

Here is my code for the service function... called service_module.py

Python Syntax (Toggle Plain Text)
  1. import time
  2. import thread
  3. import os
  4. import sys
  5. from socket import *
  6. from relocator import relocate # this function changes the cwd to the program's physical location
  7.  
  8. def handleClient(connection): # in spawned thread: reply
  9. print connection, time.ctime() # show this client's address
  10. data = connection.recv(1024) # read client socket
  11. try:
  12. response = 'Server got: ' + data
  13. except:
  14. sys.stderr.write('\n\nerror in function! ')
  15. sys.stderr.write("%s, %s\n," % tuple(sys.exc_info()[:2]))
  16. response = 'There was an error with the server.'
  17. connection.send(response) # send a response
  18. connection.close()
  19.  
  20.  
  21. class service_test:
  22. def __init__(self):
  23. thread.start_new(self.do_something, tuple()) # this is run in a thread so that the service can respond to stop requests
  24. while True:
  25. if getattr(sys,'stopservice', False):
  26. sys.exit()
  27. time.sleep(0.3)
  28.  
  29. def do_something(self):
  30. '''
  31. Do something
  32. '''
  33. myHost = '' # server machine, '' means local host
  34. myPort = 50007 # listen on a non-reserved port number
  35.  
  36. sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP socket object
  37. sockobj.bind((myHost, myPort)) # bind it to server port number
  38. sockobj.listen(5) # allow up to 5 pending connects
  39. st = '\nIm starting... ' + time.ctime() + '\n'
  40. print st
  41. print str(os.getcwd())
  42. print str(os.path.dirname(sys.argv[0]))
  43. relocate() # this just changes the cwd to the program's physical location
  44. print '\ncwd: '
  45. print str(os.getcwd())
  46. print '\n'
  47.  
  48.  
  49. while True:
  50. connection, address = sockobj.accept() # pass to thread for service
  51. print 'Server connected by', address,
  52. print 'at', time.ctime(), '\n'
  53. thread.start_new(handleClient, (connection,))
  54.  
  55.  
  56.  
  57. if __name__ == "__main__":
  58. tst = service_test()

service_module.py is imported in the following code. This code is the serviceLauncher.py... This function is sent to py2exe for compiling.

Python Syntax (Toggle Plain Text)
  1. import win32serviceutil
  2. import win32service
  3. import win32event
  4. import os
  5. import sys
  6. import time, redirect
  7.  
  8. sys.stopdriver = "false"
  9.  
  10. def main():
  11. '''
  12. Modulo principal para windows
  13. '''
  14. sys.path.insert(0,os.getcwd())
  15. import service_module
  16. a = service_module.service_test() # this is the funciton from the service_module.py
  17.  
  18. class ServiceLauncher(win32serviceutil.ServiceFramework):
  19. _svc_name_ = 'ServiceTest'
  20. _scv_display_name_ ='ServiceTest'
  21.  
  22. def __init__(self, args):
  23. win32serviceutil.ServiceFramework.__init__(self, args)
  24. self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  25.  
  26. def SvcStop(self):
  27. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  28. sys.stopservice = "true"
  29. win32event.SetEvent(self.hWaitStop)
  30.  
  31. def SvcDoRun(self):
  32. redirect.redirect(main, '') # this is a stream redirection function...
  33. # it is run like this for debugging purposes
  34. # this should not affect anything... I can supply this code too if I need to


Here is the client code...


Python Syntax (Toggle Plain Text)
  1. import sys, os, time
  2. from socket import * # portable socket interface plus constants
  3. serverHost = 'localhost' # replaced with the IP address of server prog
  4. serverPort = 50007 # non-reserved port used by the server
  5.  
  6.  
  7.  
  8. message = raw_input('Enter message: ') # default text to send to server
  9.  
  10. sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP/IP socket object
  11. sockobj.connect((serverHost, serverPort)) # connect to server machine and port
  12.  
  13. sockobj.send(message)# send message to server over socket
  14. data = sockobj.recv(1024) # receive line from server: up to 1k
  15. print 'Client received:', repr(data) # make sure it is quoted, was `x`
  16. print 'Client recieved data at', time.ctime(time.time())
  17.  
  18. sockobj.close() # close socket to send eof to server
  19.  
  20. raw_input('Press enter when done')


I hope this helps... thanks for the help
Reputation Points: 15
Solved Threads: 0
Newbie Poster
mg0959 is offline Offline
18 posts
since Jan 2008
May 29th, 2009
0

Re: Socket programming with a service

It was a firewall issue afterall
Reputation Points: 15
Solved Threads: 0
Newbie Poster
mg0959 is offline Offline
18 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: What is this code missing?
Next Thread in Python Forum Timeline: Web Spider Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC