why does no python socket port 5555 t work [duplicate] - python

I have a CherryPy script that I frequently run to start a server. Today I was having to start and stop it a few times to fix some bugs in a config file, and I guess the socket didn't close all the way because when I tried to start it up again I got this issue:
[23/Mar/2015:14:08:00] ENGINE Listening for SIGHUP.
[23/Mar/2015:14:08:00] ENGINE Listening for SIGTERM.
[23/Mar/2015:14:08:00] ENGINE Listening for SIGUSR1.
[23/Mar/2015:14:08:00] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.
[23/Mar/2015:14:08:00] ENGINE Started monitor thread 'Autoreloader'.
[23/Mar/2015:14:08:00] ENGINE Started monitor thread '_TimeoutMonitor'.
[23/Mar/2015:14:08:00] ENGINE Error in HTTP server: shutting down
Traceback (most recent call last):
File "/home/andrew/virtualenvs/mikernels/lib/python2.7/site-packages/cherrypy/process/servers.py", line 188, in _start_http_thread
self.httpserver.start()
File "/home/andrew/virtualenvs/mikernels/lib/python2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 1848, in start
raise socket.error(msg)
error: No socket could be created
I edited CherryPy's wsgiserver2.py to see the details of the socket.error and error.strerror was
98 (98, 'Address already in use') Address already in use
Meanwhile my socket is constructed as:
af = 2
socktype = 1
proto = 6
canonname = ''
sa = ('0.0.0.0', 2112)
self.bind(af, socktype, proto)
(that's not exact code but that's what the values are when the error is fired)
I checked netstat and didn't see anything listening on port 2112, what could be causing the problem and how can I go about diagnosing it?
Thanks!

You can try the following
from socket import *
sock=socket()
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# then bind
From the docs:
The SO_REUSEADDR flag tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire.
Here's the complete explanation:
Running an example several times with too small delay between executions, could lead to this error:
socket.error: [Errno 98] Address already in use
This is because the previous execution has left the socket in a TIME_WAIT state, and can’t be immediately reused.
There is a socket flag to set, in order to prevent this, socket.SO_REUSEADDR:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))

You could find the process and kill it by doing:
ps aux | grep python
, finding the process ID, and stopping it manually by doing:
sudo kill -9 PID
replacing PID with your PID.
I often have to do this while testing with Flask/CherryPy. Would be interested to see if there's an easier way (for e.g. to prevent it in the first place)

Much more easier to do it by:
Check the PID(:5000 is the host since I've been running on 127.0.0.1:5000):$ lsof -i :5000Then kill it:$ sudo kill -9 PID

Related

Python socket listening on port 80 not receiving data

I have this program which is for now supposed to only listen on port 80 and receive data either from browser connections or from another python scripts.
this code:
import socket # Import socket module
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname() # Get local machine name
port = 80 # Reserve a port for your service.
s.bind(("192.168.252.7", port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
print c.recv(1024)
c.close() # Close the connection
which is all copied from tutorialspoint. This code receives data, when the port is set to anything but 80 (eg 8080, 12345), but when it is 80, it only accepts the client but seems to not receive any data despite the data being successfully sent from somewhere else....
PLEASE HELP GUYS
Port 80 and all ports <1024 are privileged ports, your program must run as root in order to properly bind to these ports. I'm guessing you are running on Windows, since on any unix calling s.bind(("127.0.0.1", 80)) results in PermissionError: [Errno 13] Permission denied exception immediately.
I'm not sure how Windows deals with priveleged ports, but quick google search points towards windows firewall messing with your program.
Proper web servers, such as Nginx or Apache, start as root, bind to the port 80 and immediately drop to a less privileged user, since running under root is dangerous.
P.S.: A couple of suggestions:
You can skip the socket.gethostname(). Use ip 127.0.0.1 if you want your program to be accessible only from your machine, or use ip 0.0.0.0 if you want to be accessible from any machine on your network.
You should try to switch to Python 3 ASAP, since Python 2 is basically dead at this point. Don't get used to two's syntax, you gonna relearn it in a couple of years tops.

building a server, telnet localhost 8888 showing blank screen

so I'm trying to get through this tutorial here .
I started by running the code in a file here:
import socket
HOST, PORT = '', 8888
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print('Serving HTTP on port %s ...' % PORT)
while True:
client_connection, client_address = listen_socket.accept()
request = client_connection.recv(1024)
print(request.decode('utf-8'))
http_response = """\
HTTP/1.1 200 OK
Hello, World!
"""
client_connection.sendall(bytes(http_response, 'utf-8'))
client_connection.close()
then in the next part, it tells me to enter this line on the same computer.
$ telnet localhost 8888
I have webserver1.py running on a different cmd window (in windows). when I run this line though I get a blank screen instead of
$ telnet localhost 8888
Trying 127.0.0.1 …
Connected to localhost.
like I'm supposed to. Does anyone know why? How to fix it? I tried googling telnet localhost 8888 to no avail, I couldn't find a situation where this happened before.
so uh... unless there's an http daemon running/listening on that port, which couldn't happen anyway because of how ports typically work (but not always), that's actually about as far as that exercise will go.
The result you had was correct, based on the output you got after running telnet from another cmd session while that python script was running in the background. However, the windows oobe version of telnet is weaksauce. Using it to send additional commands to communicate with the server once a successful connection is established is kinda booty.
The script invokes the python interpreter (shoulda used #!/usr/bin/env python... just saying) and uses a few objects from the socket lib to open a socket on port 8888 on the localhost (the computer you're currently logged into).
That's it, nothing more. I ran through the exercise too, and gisted it # https://gist.github.com/mackmoe/09de098b3df5c45adf7a17b764a1eec4
Just my 2 cents here:
If you want to setup a server for the experience (and for free), just grab virtualbox, a linux server iso and use one of digital ocean's walkthroughs. Like the one # https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04
Hope That Helps Friend!

CherryPy fails to bind to port

Thanks for your expertise, everyone. We're running a python server using Cherrypy to expose/handle our API. This use to run fine before upgrading Ubuntu 10.10 to 11.04 (with inherent python updates), but, unfortunately, since then CherryPy does not bind to port 80 (using a proxy port 9998). The error dump is as follows:
2015-03-24 23:21:16,610 cherrypy.error - INFO - [24/Mar/2015:23:21:16] ENGINE PID 17194 written to '/var/tmp/MYSERVERNAME.pid'.
2015-03-24 23:21:16,611 cherrypy.error - INFO - [24/Mar/2015:23:21:16] ENGINE Started monitor thread '_TimeoutMonitor'.
2015-03-24 23:21:16,611 cherrypy.error - INFO - [24/Mar/2015:23:21:16] ENGINE Started monitor thread 'Autoreloader'.
2015-03-24 23:21:21,771 cherrypy.error - ERROR - [24/Mar/2015:23:21:21] ENGINE Error in 'start' listener <bound method Server.start of <cherrypy._cpserver.Server object at 0x12ffa90>>
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/cherrypy/process/wspbus.py", line 147, in publish
output.append(listener(*args, **kwargs))
File "/usr/lib/pymodules/python2.7/cherrypy/_cpserver.py", line 90, in start
ServerAdapter.start(self)
File "/usr/lib/pymodules/python2.7/cherrypy/process/servers.py", line 60, in start
self.wait()
File "/usr/lib/pymodules/python2.7/cherrypy/process/servers.py", line 101, in wait
wait_for_occupied_port(host, port)
File "/usr/lib/pymodules/python2.7/cherrypy/process/servers.py", line 266, in wait_for_occupied_port
raise IOError("Port %r not bound on %r" % (port, host))
IOError: Port 9998 not bound on '127.0.0.1'
Running netstat to see what is occupying the port shows:
alpha$ sudo netstat -pnl | grep 8080
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 17194/python
As you can see, the python server with PID 17194 starts up, starts cherrypy (which fails). I'm not sure what's colliding with what here. As you can probably tell, I'm not a server guy but that doesn't stop me from mucking around and messing things up! Anyone have a clue why CherryPy might not be binding?
I uninstalled CherryPy and reinstalled CherryPy to the latest (3.2), and the problem resolved itself.

Socket programming in python - Timestamps

from socket import *
from time import ctime
HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
print 'waiting for connection...'
tcpCliSock, addr = tcpSerSock.accept()
print "...connected from:", addr
while True:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
tcpCliSock.send("[%s] %s" % (ctime(), data))
tcpCliSock.close()
tcpSerSock.close()
I got quite some errors cleared them. I am following the Core Python Application book. I need to know what happens in the loop?
tcpCliSock, addr = tcpSerSock.accept()
what does this line of code do exactly?
'waiting for connection?' with whom and how? there are no other parameters given to connect with something!
How exactly does this listen() work?
Explain the program in short.
Thank you
What you want is described in the Python Sockets HOWTO in the documentation. Here's the Python 2 version:
http://docs.python.org/2/howto/sockets.html
The short version is that the code you posted is a simple server that is configured to respond to requests from a separate process, possibly on a different machine, that sends a small chunk of data to it. This server sends back the data with a time stamp in front.
socket.listen() is used to set up a socket to receive incoming connections. The connection requests go into a short queue and are handled in order. socket.accept() checks to see if there's an incoming connection in that listen queue, and if so, it opens up a new socket to the other machine just to handle that one connection. When the connection ends, the new socket to the first client closes and the server socket moves on to the next connection in the queue to handle it.
To make this server do anything, you'd need to use some client program to open the correct port and send a message to it. For something this simple, you might try using telnet if it's installed on your machine. Run this server, then try:
telnet localhost 21567
on the same machine and start typing some text. You should see responses. My output looked like this:
$ telnet localhost 21567
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
j
[Sun Sep 15 11:58:27 2013] j
jkl
[Sun Sep 15 11:58:29 2013] jkl
fdjksla;
[Sun Sep 15 11:58:30 2013] fdjksla;
fdjkasl;fjdksal;fjkdsa;
[Sun Sep 15 11:58:32 2013] fdjkasl;fjdksal;fjkdsa;
NOTE: You will have to put a hostname into your HOST field for this to work. Try 'localhost' and see if that helps. Also, I believe if you use localhost as your HOST parameter you may not be able to receive connections from other machines. In that instance, you'll need to use your own local hostname or IP address on the network.
I've edited the OP with fixed indenting and 'localhost' for the HOST parameter, and tested that this works with telnet in another shell as I've described here.
I notice also that this code never reaches the close() for the server socket. You'd want a break somewhere in your outer while loop if you wanted to close your server and exit in an orderly way.

Too many open files. Python multiprocess TCP server

I am writing tcp server on Python. Os - ubuntu 12.04
The server is multiprocess. Main process is accepting connections. Accepted socket reduced and sent to the worker:
<main process>
h = reduce_handle(conn.fileno())
self.queue.put(h)
Worker creates separated Thread for this connection:
<worker process>
t = threading.Thread(target=sock_thread, args=(h, DBSession, Protocol))
t.start()
Reduced socket is recovered and working at the separeted Thread:
<Connection Thread>
fd=rebuild_handle(h)
sock = socket.fromfd(fd,socket.AF_INET,socket.SOCK_STREAM)
<data transmition>
sock.close()
Everything works fine. Today I has got an exception:
error: [Errno 24] Too many open files
Restarting server solved this problem, but number of unclosed files are increased. I monitor it trow command line:
lsof | grep python | wc -l
What is the problem? I close each socket at the thread. All threads are normally works and finishes. sock.shutdown(socket.SHUT_RDWR) before socket closing raises exception: bad file descriptor.
Is there a way to close the file associated with the socket?
Thanks.

Categories