py2exe - generated executable freezes when connecting to socket - python

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.

Related

Running python in QwikLabs Linux VM through Windows PuTTY doesn't work but through mac it does. Is there a way to make it work on Windows?

I'm taking this course which requires us to ssh into a Qwiklabs Linux VM and write a python script to manipulate files and stuff on the VM. When I try ssh using PuTTY in windows 10, everything goes fine up to the point I have to run the script, then it returns invalid syntax errors. As far as I know, nothing is wrong with the code. The images linked will also show that the syntaxerrors point to stuff like valid variable names and even colons after for loops. I tested the code by trying to ssh into the VM through a mac and the script worked. Is there any way to fix this and make it run on Windows? Switching back and forth between computers is less than ideal for me and I also find it really intriguing because as far as I'm aware, it shouldn't matter which platform I'm connecting to the VM from because the python script is running on the VM. Hope someone can help, I'm a beginner and am really new to coding and ssh.python error img. Another python error img
There is nothing wrong with the VM. It is a problem with the code. From what I see in your error there is probably a unclosed parentheses above that line causing it to fail. I would need to see the code for more information.

Python 3.6 HTTP Server not reachable when compiled by PyInstaller on Windows

I created a web server in Python 3.6 using http.server.HTTPServer, http.server.SimpleHTTPRequestHandler and socketserver.ThreadingMixIn. It works as expected and I can access the webpage from any device on the local network.
I compiled it with PyInstaller to create a Windows executable. The webpage works with localhost, but it is not accessible from any device on the local network.
I used nmap from another device to scan the computer hosting the web server, and it appears that the port used by the webserver (8080) is open when I run my script normally (with the Python interpreter), and everything works. However, when I use the executable produced by PyInstaller the port isn't open and the webpage not reachable.
The executable doesn't produce any errors, and apart from that everything works.
I have tried to run the .exe file as administrator, and to this disable my antivirus/firewall. It doesn't work.
Here is my PyInstaller command :
pyinstaller --runtime-tmpdir "" --onefile -i icon.ico script.py
And here is the relevant code in my python file :
import http.server
import socketserver
port = 8080
class ThreadingSimpleServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
pass #using this so the webserver can handle mutliple requests at a time
class myWebServer(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
#does stuff
try:
server = ThreadingSimpleServer(('', port), myWebServer)
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down the program.')
server.socket.close()
I run your code and it works for me. The O.S. ask about opening the connection and everything seems to be ok. You must produce a log of some kind in order to let us understand what is going wrong. In my run i omit the -i switch because I don't have a .ico file.
Ok so I solved the issue but I still don't understand what caused it. Since the beginning I was building my script in a folder located in AppData : it wasn't working and I had no Windows firewall warning/confirmation showing up.
I moved my script to the Desktop and compiled it here : the Windows firewall confirmation showed up (this one) and the executable worked well.
I thought maybe the firewall might have flagged my program earlier and all the subsequent builds were blacklisted or something like that so I tried to compile the script in the original location, but this time I renamed it and I deleted all the files generated previously : it didn't work either (and no firewall confirmation/warning).
So now i'm pretty sure the error was caused by the firewall, and it had to do with the location from where the build was done, but I don't really understand why.

Windows 10 not allowing Python server.py to connect with client.py

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!

Execute command on remote windows machine and get output in python

I want to write a script that executes command on remote windows machine, and get output from that command. The problem is, I want to use built-in software in windows, so I can't unfortunately use for example SSH for that. I found "wmi" library, and I can log in to the machine and execute command, but i don't know how to recieve output from that command.
import wmi
c = wmi.WMI("10.0.0.2", user="administrator", password="admin")
process_startup = c.Win32_ProcessStartup.new()
process_id, result = c.Win32_Process.Create (r'cmd /c ping 10.0.0.1 -n 1 > c:\temp\temp.txt')
if result == 0:
print("Process started successfully: %d" % process_id)
print(result)
I tried to redirect output to file, but I can't find any way to get text file content either.
Is there any possible way to get output or text file content using wmi, or other python libraries?
For the application you're describing you may find RPyC is a good fit. It's a python remoting server you can connect to and issue commands; it will do anything that Python can do on the target machine. So you could, for example, use the subprocess module to run a windows command and capture the output then return the result.
The safe thing to do is to expose your remote functionality as a service -- basically, it's just a python script that RPyC can run for you. However you can also use RPyC classic mode, which is pretty much like running a python session directly on the remote machine. You should use it only when you are not worried about security, since classic mode can do anything on the remote machine -- but it's useful for prototyping.

Python Remote Command line

How do i setup a local server such that whenever someone telnets in to the server at the specified port number , a python command line must be displayed at the specified location on the remote machine so that i can remotely run my python modules.
I'm new to Python. So Please enlighten me whether this is possible. If so How?
It is more a question about how to invoke a program when someone connects to a telnet server. The program could be written in any language (including, but not restricted to python). The answer depends on the operating system of the remote server. You also have to differentiate if you want to have this behavior for all users that log in, or only to some users.
WMI might be the answer you want. And Here is a good tutorial.

Categories