I'm working a tutorial in Python 3.8 that involves sockets and networking. There is a server.py file and a client.py file. I took example code straight out of the Python doc for sockets to see if that would work, and it does not. The server starts and creates a socket and listens for the connection, but I get WinError 10061, the one where the target machine refuses the connection. My OS is Windows 10 and I'm using IDLE. I've looked at my Firewall and set a permission for pythonw.exe to be allowed through, but that has not helped. Anybody have any fixes for me to try? I can't really proceed until I can get the client and server connected.
I think I know what I’ve been doing wrong. I have been running both server and client files in the same console. I think I have to open two consoles and run one file in each so they can communicate.
(Doh!)
I’m at work so I can’t test it right now. Just in case anyone else has been befuddled by this .
Yes, I did not realize that each file had to run in its own instance of IDLE, but that makes perfect sense now. A socket won’t connect to itself!
Related
This is funny because most questions I found are the other way around.
I have a flask server that I start on debugging mode, listening on port 6000.
I have a script that accesses the url through localhost which sends back a JSON reply.
When I do an ssh tunnel using ssh -L and run the script that sends a request in my machine to localhost everything works fine. When I run the script directly from the server it also works fine. However, since I want to ultimately use a big data set, I setup a cron job. When the cron runs, the script crashes with connection refused error. I can see that the server is running after the script crashes and the port is correct on both.
I have no idea why this can happen. Any ideas are appreciated.
Solution
I couldn't figure out the reason or how to solve it, so I decided to run manually and put it in the background.
How to make a programme continue to run after log out from ssh?
A week back I posted this, and about 2 weeks back I posted this, both pertaining to an ever evolving issue with my Python sockets. I've also asked a question on the Wireshark Stack Exchange, and even after all 3 posts, I haven't been able to reach any conclusion. So heres the problem:
When I start up IDLE, it returns an error: IDLE Subprocess: socket error: No connection could be made because the target machine actively refused it and then I press ok on the dialogue boxes and it closes it out. But the issue isn't only limited to IDLE.
Any TCP ports I try to connect to via the Python socket module will fail. For example, if I run a server and a client (server first, of course, and both have the current IP of my computer and the port 45002) locally on my computer it returns: [Errno 10061] No connection could be made because the target machine actively refused it. So someone on an earlier post told me that running both the programs locally on a Windows machine will not work because it doesn't have a loopback interface (although when I look at my network interfaces via CMD, Loopback interface is listed for some reason). So then I went ahead and put the server on the computer with the issue, and the client on another computer on the network. It returns: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. But for some reason, if I run the server on the other computer and then run the client on the computer with the issue, the server and client actually connect, and then the server fails claiming: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied, even though I made sure to include a .connect(addr) in the client program. Sorry if this is getting confusing, but it's going to get a bit more complicated than this.
After this mess of running servers/clients on different computers, per request of another answerer on an early post, I installed Wireshark and began looking for packets being sent on the ports I am using. The results were a little weird. When I run the server on the computer with the issue, and the client on the other computer, Wireshark catches absolutely no packets on the port of the server, and the client fails with the errno 10060, as I said earlier. If I run the server from the other computer and the client from the computer with the issue, Wireshark catches some ordinary SYNs and ACKs. I only get an RST when I end the Python.exe process (this info is really only important to people who are good with Wireshark/networking, it's really only extra info).
I've also tried capturing packets from the IDLE program with Wireshark after I pinpointed the range of ports that it tends to use. When I start it up, no packets on any port in the range. Again, this is really just extra info to any of you who may be able to make something of it.
Again, sorry for the confusion, but I must go on.
And now, the issue that brought all this up. The problem that really caused me to ask myself "Hmm... Why is Python not working correctly?", my Twisted server was not able to import the reactor module returning the errno 10061. If you don't use/know of Twisted, all that's really needed to understand the problem is that, to import the reactor module, Twisted has to setup a Python TCP port, which is not working.
This issue has left me (and presumably everyone who's read it) scratching their head. I am sure that the ports I am using are not used. I'm sure the IP addresses are correct and matching. I am sure that there is no firewall blocking the connections, since I have tried running the programs without a firewall on with no luck. I am running a Wireless Windows 64bit laptop.
Heres my client program, and here's my server program, if they help at all.
Any suggestions, ideas, or answers are welcomed. At this point, any at all. Thanks.
P.S. If there is any info I can provide, or anything I can do to help find the solution to this issue, please tell. Also, I know this question is barely a programming one, so if there is somewhere else I am supposed to put this, please say and I'll move it over.
EDIT: Seems like I solved one piece of the puzzle. I can now connect a client on the computer with the issue to the computer with no issue, but not the other way around (running the client on the good computer, and running server on the broken one). I had to switch servSock.recv(buff) to cliSock.recv(buff) on the server side.
I'm running a local web service on Ubuntu on localhost:8090, written with bottle.py.
The connection uses SSL.
If I execute the main.py file from Nautilus or the terminal and connect to https://localhost:8090 everything works fine.
When I execute it from a link to the file, an .sh script or a .desktop file the server starts running fine, but when I browse to the address firefox says "The connection to localhost:8090 was interrupted while the page was loading"
$telnet 127.0.0.1 8090 gives this:
Trying 127.0.0.1...
Connected to 127.0.0.1...
Escape character is '^]'.
Connection closed by foreign host.
$sudo netstat -ntlupp | grep 8090 gives this:
tcp 0 0 127.0.0.1:8090 0.0.0.0:* LISTEN
iptables is default
I've got the feeling it's blocking the connection when the server is executed "indirectly" (link, script or .desktop), since when I actually click on the file or run it through terminal it runs fine.
I don't have a clue on where to prevent it from blocking the connection, though. Any help is greatly appreciated.
Any workaround will do, even just pretending the file is being run directly from the user.
Thanks in advance
Watch the server logs.
The major difference between the different methods of invocation probably is the current working directory.
I think that it is unlikely that the network configuration is involved in what you are observing.
Depending on the complexity of your web application it might be that a Python import fails if the main script is not run from the right directory. This would trigger a Python exception, which might lead to an immediate connection reset. I have not worked with bottle, but other Python web frameworks distinguish a development mode in which Python tracebacks are shown in the browser, and a production mode in which an HTTP error is sent to the client.
This is what you should do in order to debug your issue: run your server from a terminal (cd to the right directory, then run python application.py). Carefully watch stdout and stderr of that server process while connecting to the web application with your browser.
Ok, problem solved.
It was actually depending on the current working directory not being the same as the python file running the WSGI server.
If I run the .sh script or the link from the same directory everything works fine, and if I give a cd command in the script everything works smoothly.
Thanks for the help Jan-Philip!
I created a simple server and client using Python. When i run it on my computer i works fine. But if i run the server on my comupter and try to open client on the other computer, it cant find my server.
Thanks for helping.
Do you know the IP for your server computer? Just making sure you know that 127.0.0.1 (or localhost) no longer works in this setup.
Are your computers behind a NAT?
Can you ping from one computer to another?
Do you have a firewall?
For BaseHTTPServer http://docs.python.org/library/basehttpserver.html you will want to pass in the empty string '' as the server address.
Pardon my ignorance as I'm still a beginner in coding.
I'm trying to convert a python script I wrote to a Windows executable program using py2exe. However, though I am able to successfully convert the script, the executable doesn't seem to be fully functional.
After much debugging, I have isolated the cause and the following code seems to be the problem
host = str(raw_input('Enter Host IP Address: '))
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, 5000))
The problem does not occur when the script is executed from Pydev itself and the script is able to run without problems. The windows executable which is a console application just hangs when trying to connect to another host.
Is this a known issue or am I doing something wrong? Any help is much appreciated.
Are you able to input the IP address? Reading that thread it seems that py2exe requires a special windows argument to launch a console. Otherwise, raw_input tries to read from the standard input, and hangs/crashes because it does not find anything.
Given the age of the thread, I checked py2exe doc: you might want to try to put your script in the console attribute.
I really think that the behavior is related to raw_input, and that it is not caused by the socket operation.