2

So first off, let me show you my code and the error it returns:

print "before import"
from twisted.internet import protocol #  imports
print "after protocol"
from twisted.internet import reactor
print "after reactor"
from twisted.internet.endpoints import TCP4ServerEndpoint
print "after import"

class Echo(protocol.Protocol):
    """docstring for Echo"""
    def connectionMade(self):
        cADDR = self.clnt = self.transport.getPeer().host
        print "...Connection made with {0}".format(cADDR)
    def dataReceived(self, data):
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    """docstring for EchoFactory"""
    def buildProtocol(self, addr):
        return Echo()


server = TCP4ServerEndpoint(reactor, 45002)
server.listen(EchoFactory())
reactor.run()

As you can see, I created some print statements to debug exactly which import is causing the issue. Now for the error:

before import
after protocol
Traceback (most recent call last):
  File "C:\Users\Sa'id\Documents\Learning Programming\Python\Core Python Application Programming\Chapter 2 - Network Programming\Twisted\twisted_intro.py", line 9, in <module>
    from twisted.internet import reactor
  File "C:\Python27\lib\site-packages\twisted\internet\reactor.py", line 39, in <module>
    default.install()
  File "C:\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 196, in install
    reactor = SelectReactor()
  File "C:\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 72, in __init__
    posixbase.PosixReactorBase.__init__(self)
  File "C:\Python27\lib\site-packages\twisted\internet\base.py", line 499, in __init__
    self.installWaker()
  File "C:\Python27\lib\site-packages\twisted\internet\posixbase.py", line 286, in installWaker
    self.waker = self._wakerFactory(self)
  File "C:\Python27\lib\site-packages\twisted\internet\posixbase.py", line 81, in __init__
    client.connect(server.getsockname())
  File "C:\Python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
>>> 

For some reason, my Twisted server is trying to make connections, when in reality, it should be the one waiting for the connections, not making them. And as you can see from the error, it prints right before the reactor import, but not after it, so the reactor is really the issue here. I've posted this on another website without much success, but the replier said that, it was because the reactor was trying to setup a _SocketWaker and something was blocking it from setting it up. He said that turning off your firewall would make it work, but after trying it, the same error was returned. Just a note, the port I am hosting this Echo() server on is port forwarded, so the port is probably not the issue. Any input would be much appreciated.

Thanks.

said
  • 474
  • 4
  • 8
  • 18
  • Have you tried to allow connections from localhost to localhost? The `reactor` might need them to implement "self-pipe trick": `signal.set_wake_fd` functionality that works only for sockets on Windows and the signal support is limited on Windows. Here's [code example for POSIX where `signal.set_wake_fd` is used with a pipe](http://stackoverflow.com/q/30087506/4279) – jfs Jul 12 '15 at 10:52
  • @J.F.Sebastian, I appologize, but I am very new to the network programming area as a whole, so I am having a little trouble understanding what I am supposed to do. If you don't mind, can you break it down a little more for me, and what I should do next (I don't need any code, just a more detailed nod in some direction). Thanks. – said Jul 13 '15 at 07:42

1 Answers1

1

On UNIX, Twisted sets up a thread-waker file descriptor using a pipe. However, on Windows, anonymous pipes have several implementation issues and discrepancies between different Windows versions, so it uses a socket pair. Creating this socket pair involves connecting back to localhost, so, certain overly-aggressive firewall configurations can trigger this area.

Glyph
  • 31,152
  • 11
  • 87
  • 129
  • I have tried completely turning off my firewall with no luck. I'll try again right now and see if anything changes. Thanks for the input. EDIT: Just tried running again after turning off the firewall, the reactor still refuses to import and the same error is returned. – said Jul 13 '15 at 07:44
  • Do you have an entry in `C:\Windows\System32\Drivers\etc\hosts` that points `localhost` at something other than `127.0.0.1`, perhaps? Can you connect to other locally bound ports using other programs? Is `python.exe` restricted from performing networking entirely somehow? – Glyph Jul 14 '15 at 10:29
  • At a python prompt, does `socket.socketpair()` work? – Glyph Jul 14 '15 at 10:30
  • For your first comment, [this is my file](http://pastebin.com/Zp9nQNdq). Everything in there is 127.0.0.1. Also, none of my addresses (IPs) are 127.0.0.1, so I'm not sure why these are 127.0.0.1. And for your second comment, no, socket.socketpair() does not work. Returns an AttributeError and says that it's not a part of the socket module. And lastly, I'm running Python 2.7. – said Jul 14 '15 at 22:46
  • I think there might be a bug in Twisted here, but I'm not sure why your system is hitting this code path. You should probably remove those entries from your `hosts` file (on a new windows system, it has only comments in it), but before you do, what does `python -c "import socket; print(socket.socket().getsockname())"` produce? Those addresses are set to 127.0.0.1 presumably to prevent internet resources from loading; in IPv4 that address means "the current host": https://en.wikipedia.org/wiki/Localhost – Glyph Jul 15 '15 at 23:02
  • It returns this: `socket.error: [Errno 10022] An invalid argument was supplied`. So should I go ahead and remove all those lines? Also, thanks for the info (and link). – said Jul 16 '15 at 22:16
  • Yes, I think you should. One more question before you do though: what does `python -c "from socket import socket; s = socket(); s.bind(('127.0.0.1', 0)); print(s.getsockname())"` print out? – Glyph Jul 17 '15 at 23:29
  • Prints out: `('127.0.0.1', 55156)`. So should I just delete all those lines? – said Jul 18 '15 at 03:13
  • Yeah, delete those lines. Although if your firewall is off I really cannot imagine what would prevent it from working, even with those in place; something like that result is exactly what I would expect on a working system. – Glyph Jul 20 '15 at 22:44
  • Deleted all lines, still nothing. – said Jul 21 '15 at 16:07
  • I'm gonna go ahead and repost this, as I don't have the ability to add bounty. Maybe it will get some traction. Thanks for all the help. Gonna make it more general, since Yesterday I finally connected all the dots. It's not my computer, but rather, Python's socket. IDLE won't start up because it encounters a Socket Error. And If I make a TCP server/client, and run them both locally, the client will return the same error as I mentioned in the OP. UDP seems to work though. – said Jul 22 '15 at 16:13
  • Ultimately the issue is that you need to disable your firewall. If you've disabled the main one that you know you're using, there is some other firewall, or perhaps the built-in Windows firewall, which is not letting Python make socket connections to localhost. Any piece of software which can prevent connections to 127.0.0.1 is by definition a firewall, even if it doesn't advertise itself as one. So this is really more of a Windows administration question than a programming question. – Glyph Jul 22 '15 at 17:43