Problems running python program with srvany.exe - python

I must preface this with a full disclaimer that i'm very early in my python development days
I've made a simple python program that waits for a socket connection to the local ip address over port 20000. When it gets a connection, it pops up a message alert using the win32api.
#tcpintercomserver.py
import socket
import sys
import win32api
ip = socket.gethostbyname(socket.gethostname())
#socket creation
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Binding
server_address = (ip, 20000)
sock.bind(server_address)
print server_address
#Listen
sock.listen(1)
while True:
# Wait for a connection
connection, client_address = sock.accept()
win32api.MessageBox(0,'MessageText','Titletext', 0x00001000)
# Close Connection
connection.close()
I also have a mated client program that simply connects to the socket. The script takes an argument of the host you're trying to reach (DNS name or ip address)
#tcpintercomcli.py
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = (sys.argv[1], 20000)
sock.connect(server_address)
This all runs fine as scripts. I then used CX_Freeze to turn them into executables. Both run just like they did when they were scripts.
Now i've taken the server script and connected it to a service with srvany.exe and use of the SC command in windows.
I set up the service using SC create "intercom" binPath= "C:\dist\srvany.exe"
Under the intercom service key in the registry, i've added the Parameter's key, and under there set Application to a string value c:\dist\tcpintercomserver.exe
I then perform a "net start intercom" and the service launches successfully, and the tcpintercomserver.exe is listed as a running process. However, when i run the tcpintercomcli.py or tcpintercomcli.exe, no alert comes up.
I'm baffled...is there something with the CX_Freeze process that may be messing this up?

Service process cannot show messagebox, they don't have access to UI, they usually run as SYSTEM user. if you are running from service, proper way of debugging and showing messages are using EventLog.
See:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28VS.71%29.aspx

If you are on Windows Vista or later, your script is running headlong into Session 0 Isolation -- where GUI elements from a Windows service are not shown on an interactive user's desktop.
You will probably see your message box if you switch to session 0...

Related

Firewall is not allowing my python client application to connect to a server running on my machine even after I turn off firewall completely

I am learning about the python socket library and am running into problems whenever I try to connect to the server running on my localhost with a client application.
Here is the server code:
import socket
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
print(data)
Here is the code for my client application:
import socket
HOST = "localhost" # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
Here is my error message:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Here is what I have tried so far:
Disabling my Window's 10 firewall completely on the windows command prompt with the use of the following command:
netsh advfirewall set allprofiles state off. This did not work
To windows firewall I added an inward rule and outward rule that allows any application on my OS to access a service running on port 65432
I changed my python version from 3.8.2 to 3.7.7 because before hand I was able to run this code perfectly and I was using a python 3.7 version
I tried multiple different methods of setting the HOST variable, which include "localhost", '127.0.0.1', socket.gethost(), and socket.gethostbyname("localhost")
I am able to connect to the server through the use of the Window's telnet application but that is it. To be honest I have exhausted possible solutions that I can find online, and I know that this question has came up on this website a lot, but I have honestly tried every solution I have seen so far - which included three hours of searching.
I appreciate any possible help that you guys can give, thanks.
Since the code was working earlier in the machine,this doesn't seem to be code issue.
Also the code ran fine in my machine.
I suggest you to run through the below steps once again:
Solution 1:
1. edit the server address as 127.0.0.1 or the host private IP in both the code just to be assured there is no discrepancy.
2. Start the server program first and make sure it didn't terminate.
3. Start the client application and check if the server program threw any error or exceptions.
Solution 2:
Change the port number and follow solution 1.
Solution 3:
Switch off the windows firewall from the UI just to be sure.
Follow the solution 1 steps
Solution 4:
Change the server address as host=''

Sending Terminal commands from a running script on remote back to my local?

I'm developing on a remote server which I login using ssh and develop using vi. I however need to send Terminal notification commands osascript -e "display notification {} {} {}" and such commands back to my local terminal so I can get sound/mac notifications on my system. How do I achieve this?
I know I can use import os; os.sytem('command') for the script on server to send terminal commands on the machine its running in i.e., the server itself, but is there a similar command to send commands back to my local ? Ideally, I need this to be done from the scripts itself- because I have multiple triggers for notifications to be done.
You need to use some sockets
On your server machine you need something like this:
import socket
IP = "0.0.0.0" # Your Local Machine IP
PORT = 5200 # Your Local Machine Listening Port
def send_message(msg):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IP, PORT))
s.send(bytes(msg, 'UTF-8'))
data = s.recv(4096)
s.close()
print(data)
You can use the method where ever you need, the only argument it takes is msg, simply it's the command you need to send to your local machine
On your local machine this script should do what you need:
import socket
import os
IP = "0.0.0.0" # 0.0.0.0 Means every available IP to assign, but you need to use your external IP on the script that u will use on server
PORT = 5200 # The port you want to listen on
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((IP, PORT))
s.listen(1)
while True:
conn, addr = s.accept()
print("Command from {}".format(addr))
data = conn.recv(4096)
if not data: continue
if data == b'stop': break # This line just defines a word that will make your local machine stop listening.
print(data)
command = data.decode('UTF-8')
os.system(command)
conn.send(data)
conn.close()
You need to run The local machine script first

Communicating between processes on different computers

I wanted to communicate between processes (one process does something, sends results to other process which does something with it).
So I used this code:
Server:
from multiprocessing.connection import Listener
address = ('localhost', 6000) # family is deduced to be 'AF_INET'
listener = Listener(address, authkey='secret password')
conn = listener.accept()
print 'connection accepted from', listener.last_accepted
while True:
msg = conn.recv()
# do something with msg
if msg == 'close':
conn.close()
break
listener.close()
Client:
from multiprocessing.connection import Client
address = ('localhost', 6000)
conn = Client(address, authkey='secret password')
conn.send('close')
conn.close()
(source: interprocess communication in python)
And it works like a charm. But I wanted to run these two programs from another computer. On Comp. A I have these 2 programs. I connect to Comp A from Comp B by Wifi Lan (using ssh connection) and I run these 2 programs (which means they're running on Comp A), but they don't connect with each other. I've tried using wifi lan address (192.168.x.x) instead of "localhost" but it didn't work neither. What parameter do I have to use instead of "localhost" so that these 2 programs can connect. Or what is the easiest way to do this.
Cheers!
Go to command prompt and type ipconfig on the other computer. You need to use the IPv4 address. You also need to make sure the ports are open and ssh enabled. replace the 'localhost' with the IPv4 address.

Connecting to a simple sockets python server remotely

I am trying to setup a very simply sockets app. My server code is:
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.bind((host,port))
s.listen(5) #Here we wait for a client connection
while True:
c, addr = s.accept()
print "Got a connection from: ", addr
c.send("Thanks for connecting")
c.close()
I placed this file on my remote Linode server and run it using python server.py. I have checked that the port is open using nap:
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
1234/tcp open hotline
I now run the client.py on my local machine:
import socket # Import socket module
s = socket.socket() # Create a socket object
port = 1234 # Reserve a port for your service.
s.connect(("139.xxx.xx.xx", port))
print s.recv(1024)
s.close # Close the socket when done
However I am not getting any kind of activity or report of connection. Could someone give me some pointers to what I might have to do? Do I need to include the hostname in the IP address I specify in the client.py? Any help would be really appreciated!
I've just summarize our comments, so your problem is this:
When you trying to using the client program connect to the server via the Internet, not LAN.
You should configure the
port mapping on your router.
And however, you just need configure the
port mapping for your server machine.
After you did that, then you can use the client program connect to your server prigram.

Errno 10061 : No connection could be made because the target machine actively refused it ( client - server )

I have a problem with these client and server codes, I keep getting the [Errno 10061] No connection could be made because the target machine actively refused it
I'm running the server on a virtual machine with Windows XP SP3 and the client on Windows 7 64bit, my python version is 2.7.3. What I want to know is how should I edit the code to use the client and server on different networks! Thanks!
server :
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = '0.0.0.0' # Get local machine name
port = 12345 # Reserve a port for your service.
print 'Server started!'
print 'Waiting for clients...'
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
while True:
msg = c.recv(1024)
print addr, ' >> ', msg
msg = raw_input('SERVER >> ')
c.send(msg);
#c.close() # Close the connection
client :
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
print 'Connecting to ', host, port
s.connect((host, port))
while True:
msg = raw_input('CLIENT >> ')
s.send(msg)
msg = s.recv(1024)
print 'SERVER >> ', msg
#s.close # Close the socket when done
PS : code is from internet.
10061 is WSAECONNREFUSED, 'connection refused', which means there was nothing listening at the IP:port you tried to connect to.
There was a firewall product around the year 2000 that issued refusals instead of ignoring incoming connections to blocked ports, but this was quickly recognised as an information leak to attackers and corrected or withdrawn.
Hint: actively refused sounds like somewhat deeper technical trouble, but...
...actually, this response (and also specifically errno:10061) is also given, if one calls the bin/mongo executable and the mongodb service is simply not running on the target machine. This even applies to local machine instances (all happening on localhost).
➪ Always rule out for this trivial possibility first, i.e. simply by using the command line client to access your db.
See here.
So I was facing the same issue,
and the solution that worked for me was...
I am assuming your server and client program are written in python.
First, open one python shell
open and run the Server program first
then open another different python shell
open and run the Client program here
done !!
Using the examples from: https://docs.python.org/3.2/library/socketserver.html
I determined that I needed to set the HOST port to the machine I had the server program running on. So TCPServer on 192.168.0.1 HOST = TCPServer IP 192.168.0.1 then I had to set the TCPClient side to point to the TCPServer IP. So the TCPClient HOST value = 192.168.0.1 - Sorry, that's the best I can describe it.
There is no relationship between error and firewall.
first, run server program,
then run client program in another shell of python
and it will work
instead of localhost of '0.0.0.0', use local network address as host in case of both - the server and the client - code.
host = '192.168.12.12'
port = 12345
use this host address when binding and connecting to the socket.
server.bind((host, port))
client.connect((host, port))
this change solved the issue for me.
The solution is to use the same IP and Port number in both client and server.
Try, in client to use
TCP_IP = 'write the ip number here'
TCP_PORT = writ the port number here
s.connect((TCP_IP, TCP_PORT))
if you have remote server installed on you machine. give server.py host as "localhost" and the port number.
then client side , you have to give local ip- 127.0.0.1 and port number.
then its works
I was facing a similar problem when I was calling REST API using python library and what I found that my server was going into sleep mode which was leading to this. As soon as I logged in to the server via Remote Desktop Connection, my API call used to work.
This could be because of proxy or firewall. If it's proxy, then you need to specify proxy setting at entry point of your code or project.
import os #for proxy
proxy = 'http://10.XX.XX.XX:8X8X' #your own proxy 'http://<user>:<pass>#<proxy>:<port>'
os.environ['http_proxy'] = proxy
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy
#rest of code .....
The below changes fixed my problem.
I struggled with the same error for a week. I would like to share with you all that the solution is simply host = '' in the server and the client host = ip of the server.  
The first: Please make sure your port '12345' is opening and then
when you using a different network. You have to use the IP address in LAN. Don't use the 'localhost' or '127.0.0.1'.
The solution here is:
In server
host = '192.168.1.12' #Ip address in LAN network
In client
host = '27.32.123.32' # external IP Address
Hope it works for you
I had errors 10060 and 10061. The reason was in my antivirus(Eset Nod 32). Try to turn off the Firewall of your antivirus as I did or just delete it for a time to test the program. If everything started to work properly, add that program to the exclusion or switch to another antivirus.
Also, try to change the 'host' variable to an empty string:
host = ''
And add socket.AF_INET, socket.SOCK_STREAM to the 's' variable:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
I was doing this tutorial and they said that windows users will have a problem. They said that you can check the Windows Firewall to fix the problem. Let me show you a quick Google Search on how to change the windows firewall:
Go to Start and open Control Panel. Select System and Security > Windows Defender Firewall. Choose Turn Windows Firewall on or off. Select Turn on Windows Firewall for domain, private, and public network settings.
After that, your app should work. Also, in your client(not server side) the port should be 0.0.0.0 and in the server side, it should be 127.0.0.1.
If the client and server are running on the same machine (as in running 2 programs), it's OK to config both IP address as locahost. However, if you are to run them on different machines (including VMs), you need to
Make sure they are on the same subnet (usually by pinging each other)
The Server needs to config the host IP as its IP address (like 192.168.xxx.xxx instead of localhost or 127.0.0.1). You may find the IP address by running ipconfig on Windows or ip a on Unix-like server
This change worked for me with my Client on Windows and Server on Ubuntu VM.
Some of the other solutions will work if you want to run server.py and client.py on the same machine. I wanted to try and run it on two different machines (windows and raspberry pi), but on the same network.
For me, it was a matter of choosing the correct IP address. If my windows machine is the server, I used the IPV4 address of the windows machine. This can be found by running ipconfig in the command prompt and selecting the 192.168.X.X number. The raspberry client side bounded to the same address. If the raspberry pi is the server, then I would bind to the inet address. You can find this by running ifconfig in the terminal (again the 192.168.X.X).
Note though, the IP addresses are temporary. I believe if you want a more permanent set-up, the server IP address needs to be bound to the router's IP address, then port-forward to the server. That way, the client wouldn't even have to be on the same network.
First you have to start your server( run server.py ) using Command prompt and after that you can easily run client.py because you need a server first which will host so that client.py could be run.
the short term solution is to use the default iis host and port normally 120.0.0.1 and 80 respectively. However am still looking for a more versatile solution.
When you run the code on windows machine, firewall prompts it to allow network access, allow the network access and it will work, if it does not prompts, go to firewall settings > allow an app through firewall and select your python.exe and allow network access.

Categories

Resources