Python - error with a simple socket script - python

So I wrote a script in Python with the module socket but finally he doesn't work. After searching a few hours for an error (I'm new in Python so it's long for me), I didn't find any so instead I wrote another simple script which works with netcat to have a reverse shell between my host machin (Linux) and my VM (Windows).
My command netcat with Linux :
nc -nvlp 4444
My script in Windows :
import socket
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(("192.168.1.81", 4444))
And after start the listener (netcat), he's listening and after a few seconds, in my Windows machin, the scripts closed itself because he doesn't find anything connection.
I don't know where is the problem, my VM is on the same subnet than my host, I've flushed my iptables in Linux just in case, I'm lost :)
Thanks for your answers !

I just send the script to a friend for trying and it's working, so I think so the problem is in my settings, or somewhere else..

Related

I cannot connect to my own IRC server with python socket

I am Japanese and I am not good at speaking English. Please let me know if there is anything I say that you don't understand.
I have a question regarding Python's socket module.
I am setting up an IRC server on a virtual machine and I am trying to create an IRC bot with Python, but I am unable to connect to the server.
I have tried the following minimal code:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5) # set timeout to 5 seconds
sock.connect(('xxx.xx.xxx.xx', 6667))
However, it times out and I am unable to connect. I have checked the server's IP address using ping and it returns a response. The virtual machine is running CentOS 7 and both SElinux and firewall are disabled. Ruby code works properly and LimeChat2 on my local PC can also connect without any issues.
Please let me know what could be the possible reasons for this issue. The port number is correct.

TCP server (Python) on Raspberry Pi bootup results in error: Address already in use

I have a TCP server created on a Python script, I want to execute it on Raspberry Pi boot up. I have put the Python script in .bashrc and it executes on the boot up, but it displays the following error:
s.bind(('', 5555))
socket.error: [Errno 98] Address already in use
I have created a TCP client on Matlab and the communciation works perfectly. I can manually execute the server from Raspberry Pi 3 command line send/recv data and close the socket, but even after properly closing if I restart my Raspberry Pi, on boot up it still displays the error.
I have also tried s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1), but I still get the error. I dont want to manually change the port number every time, therefore any help would be appreciated.
This message occurs when you try to use a port number that is already in use.
Because you start the server process with your .bashrc file, an attempt will be made to start it when your system boots, when you start a new shell, or when you ssh into the system.
This article makes some suggestions about other ways you could use.
Note that when you see this message, it comes from repeated attempts to start your server, but the good news is you should still be able to connect to the server - the message does imply it's already running.

why connection always gets refused?

I am trying to connect machine using the RPyC but it always says that connection refused.
I did on the python shell
import rpyc
rpyc.connect("hostname", port)
but it says connection refused. checked the firewall for the port. firewall allow this port.
Try using the exact same versions of both python and rpyc on client and server !
This means that you are not runnin the server side for rpyc
you need to donwload the source code for rpyc from here
https://github.com/tomerfiliba-org/rpyc/releases
then run:
python bin/rpyc_classic.py
where bin is in the source code folder
Once you have that running, you should be able to run the python code without any issues
I hope it works
default server binds to localhost, but the client needs to have hostname 'None' to do this correctly:
rpyc.connect(None, port)

How to properly open Port 8888 in Ubuntu under VirtualBox?

I am working on a Python Flask application in Ubuntu under VirtualBox, run from OSX. Now I want to connect my python application from OSX (the host OS). I am using a NAT networking, and already read about Port Forwarding (which I think it's very easy). But, somehow if I test my application using Google Chrome, try to open http://localhost:8888 (my server in Ubuntu listen to port 8888), chrome return with Server return no response (or kinda)
I have issue the following command in OSX terminal : `VBoxManage modifyvm "Ubuntu 12.0.4 LTS" --natpf1 "fikrposdc,tcp,,8888,,8888". And my python application already listening to port 8888, :
* Running on http://127.0.0.1:8888
I've read that in Ubuntu, by default there is no firewall. So, I try to netstat, but issuing `netstat | grep LISTEN didn't gave any output...
Is there something that I missed?
`
Wooops!
Having read related question, I found this question with the exact same problem. And I modify my code to run the application :
app.run(host="0.0.0.0", port=8888)
Now I can continue my work!
Thanks

Connecting to Python XML RPC from the Mac

I wrote an XML RPC server in python and a simple Test Client for it in python. The Server runs on a linux box. I tested it by running the python client on the same linux machine and it works.
I then tried to run the python client on a Mac and i get the following error
socket.error: (61, 'Connection Refused')
I can ping and ssh into the linux machine from the Mac. So i dont think its a configuration or firewall error.
Does anyone have any idea what could be going wrong?
The code for the client is as below:
import xmlrpclib
s = xmlrpclib.ServerProxy('http://143.252.249.141:8000')
print s.GetUsers()
print s.system.listMethods()
"Connection Refused" means the connection was REFUSED - the machine 143.252.249.141 is up, and in the network, but is not accepting connections on port 8000 - it is actively refusing them.
So maybe the server software isn't running on the server? Or is running in another port? Or is bound to a different IP address?

Categories

Resources