I'm trying to bind WSGIServer to a IPv6 loopback address:
import socket
import gevent
from gevent.pywsgi import WSGIServer
addrs = socket.getaddrinfo('::1', 8000, socket.AF_INET6, 0, socket.SOL_TCP)
bind_spec = addrs[0][-1]
listener = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
listener.bind(bind_spec)
server = WSGIServer(
listener,
lambda a,b:'asd'
)
server.serve_forever()
But this only yields
error: [Errno 22] Invalid argument
in
File "/usr/local/lib/python2.7/dist-packages/gevent/server.py", line 122, in _do_accept
client_socket, address = self.socket.accept()
I've tried to use gevent monkeypatcher, that didn't help. Importing gevent AFTER socket creation didn't help either.
IPv6 is enabled on my system. If I replace server construction with plain accept(), socket binds successfully and accepts connections.
listener.listen(5)
listener.accept()
I'm so stupid... GEvent doesn't invoke listen() on the provided socket, so I had to call it myself before creating the server.
Related
So before anyone says its a duplicate, I have seen multiple questions with that error, but could not notice any of that being the same as my problem.
I am trying to make a small project including a socket over SSL, and when trying to catch if a user is trying to connect with a raw socket and not ssl wrapped socket (which is raising a ConnectionResetError) I get a different error.
My code:
import socket
from classes import ClientThread
import ssl
from time import sleep
server = 'localhost'
port = 12345
threads = []
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.load_cert_chain(certfile="cert.pem", keyfile="cert.pem")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((server, port))
print(f"[*] server started, listening on port {port}")
while True:
s.listen()
with context.wrap_socket(s, server_side=True) as ssock:
try:
conn, addr = ssock.accept()
client = ClientThread(conn=conn, ip=addr[0], port=addr[1])
client.start()
threads.append(client)
print(f'Threads running: {len(threads)}')
except ConnectionResetError:
print(f'Could not establish ssl handshake with a client.')
The error i get is:
Traceback (most recent call last):
File "C:/Users/x/x/server.py", line 17, in <module>
s.listen()
OSError: [WinError 10038] An operation was attempted on something that is not a socket
I tried setting some sleep time after the exception maybe it needed to reset the socket but didnt hlep, tried to play a bit with the placement of the While True, and while resetting the entire socket help, I dont want to reset all my clients thread just because of a client who didnt try to log in with a SSL socket.
I think it has something to do with the wrap_socket because it modified the socket instance passed to it , but couldnt find a way to unwrap.
Thank you in advance!
listen enables a socket to take incoming connection requests (also called a "passive socket") and establishes a backlog of how many of those requests can be pending in the network stack at any given time. accept accepts one of those connections. You call listen once and accept many times.
Pull the listen outside of the while so that is only called once to establish this as a listening socket.
Following is the Server and Client code written for Python 3
Server code:
import sys
from socket import socket, gethostbyname, AF_INET, SOCK_STREAM
PORT_NUMBER = 5060
SIZE = 1024
hostName = gethostbyname( '0.0.0.0' )
recvSocket = socket( AF_INET, SOCK_STREAM )
recvSocket.bind((hostName, PORT_NUMBER))
recvSocket.listen(5)
print("Listening for client...")
(conn,addr)=recvSocket.accept()
print ("Test server listening on port {0}\n".format(PORT_NUMBER))
print("Connected to client at address {0}\n".format(addr))
print("Connection is ",conn)
Client Code:
import sys
from socket import socket,AF_INET,SOCK_STREAM,gethostbyname
hostname=gethostbyname('0.0.0.0')
print ("Creating")
sendsocket=socket(AF_INET,SOCK_STREAM)
print ("Connecting socket")
sendsocket.connect(('192.168.4.39',5060))
print ("connected")
data=input("Enter value")
sendsocket.sendto(data.encode('utf-8'),(SEND_IP,SEND_PORT))
Now the situation that I am facing is as follows:
1st Scenario
Server code is running on a Windows System and the client code is
running on a Linux system
Result: Client code is getting stuck after printing "Connecting socket"
2nd Scenario
Server Code is running on a Linux System and the Client code is running on
Windows system
Result: Getting output as Expected.
Why is this behaviour occurring? Does connect() function has any kind of problem or the code is having some problem?
NOTE: Both Server and Client system is on the same Network. 192.168.4.39 is the IP Address where the server code is running.
First make sure client and server connected well.try ping each other.
Then do it manually like this:
Ssocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
should work
I was trying to write a code where a client connects to server on a default port number, the server then sends another port number to the client. The client now connects to the new port number.
Client:
import socket
import sys
import os
import signal
import time
s = socket.socket()
s.connect(("127.0.0.1", 6667))
line = s.recv(1024)
if line.strip():
port = int(line)
s.close()
soc = socket.socket()
soc.connect(("127.0.0.1", port))
print soc.recv(1024)
soc.close()
else:
s.close()
Server:
import socket
import sys
import os
import signal
import time
port = 7777
s = socket.socket()
s.bind(("127.0.0.1", 6667))
s.listen(0)
sc, address = s.accept()
print address
sc.send(str(port))
sc.close()
s.close()
sock = socket.socket()
sock.bind(("127.0.0.1", port))
soc, addr = sock.accept()
print addr
soc.send("Success")
soc.close()
sock.close()
When I execute this code, I am getting following errors on client and server sides.
Server:
('127.0.0.1', 36282)
Traceback (most recent call last):
File "server.py", line 17, in <module>
soc, addr = sock.accept()
File "/usr/lib/python2.7/socket.py", line 202, in accept
sock, addr = self._sock.accept()
socket.error: [Errno 22] Invalid argument
Client:
Traceback (most recent call last):
File "client.py", line 13, in <module>
soc.connect(("127.0.0.1", port))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused
Can someone explain me the reason for these errors and provide a solution for these errors.
Before you can listen to a TCP/IP socket (a connection based streaming socket) you need to use bind to assign a socket (created with socket.socket()) . Then you need to do listen to prepare it for incoming connections and then finally you do accept on the prepared socket.
You appear to be missing sock.listen(0) after your call to sock.bind(("127.0.0.1", port)). The Python documentation is short on details but it does say this about TCP/IP:
Note that a server must perform the sequence socket(), bind(), listen(), accept() (possibly repeating the accept() to service more than one client), while a client only needs the sequence socket(), connect(). Also note that the server does not sendall()/recv() on the socket it is listening on but on the new socket returned by accept().
Python bases its socket module on a Berkeley Socket model. You can find some more detailed information on Berkeley Sockets at this link . In particular it says this about bind:
bind() assigns a socket to an address. When a socket is created using socket(), it is only given a protocol family, but not assigned an address. This association with an address must be performed with the bind() system call before the socket can accept connections to other hosts.
Also consider what would happen if your client gets sent a port number (and tries to connect) before the server starts listening for connections (on port 7777 in this case). Although not the cause of your problems, I wanted to point out the scenario for completeness. Something you may consider is not closing the port 6667 socket until after you have called listen on the port 7777 socket. After calling listen you can then close down the first socket. On the client after reading the port you can wait until the first connection (port 6667) is closed down by the server and then connect to port 7777.
I am writing server application in Python that listens for requests, processes them, and sends a response.
All req/resp are send from the same address and port to the server application. I need to recv/send messages simultaneously, and the server need to recieve/send messages from/to the same port and address. I found some tutorials for asynchore sockets, but there are only examples for TCP connections.
Unfortunately, I need UDP. When I change SOCK_STREAM to SOCK_DGRAM in the create method, I get an error.
return getattr(self._sock,name)(*args)
socket.error: [Errno 95] Operation not supported
I tried to use twisted, but I dont know how to write the sender part, which can bind to the same port as its listening. The last result was blocked port.
Is there any way how to use asyncore sockets with UDP or how to use twisted to send from the same port? Some examples will be higly appreciated.
You can pretty much just write the sending and receiving part of your code and they'll work together. Note that you can send and receive on a single listening UDP socket - you don't need one for each (particularly if you want to send from and receive on the same address).
from __future__ import print_function
from sys import stdout
from twisted.python.log import startLogging
from twisted.internet import reactor
from twisted.internet.protocol import DatagramProtocol
class SomeUDP(DatagramProtocol):
def datagramReceived(self, datagram, address):
print(u"Got a datagram of {} bytes.".format(len(datagram)))
def sendFoo(self, foo, ip, port):
self.transport.write(
(u"Foo datagram: {}".format(foo)).encode("utf-8"),
(ip, port))
class SomeSender(object):
def __init__(self, proto):
self.proto = proto
def start(self):
reactor.callLater(3, self._send)
def _send(self):
self.proto.sendFoo(u"Hello or whatever", b"127.0.0.1", 12345)
self.start()
startLogging(stdout)
proto = SomeUDP()
reactor.listenUDP(12345, proto)
SomeSender(proto).start()
reactor.run()
So I have a web server that I can run using python, but I have a question. Can I change the IP address where the server runs the only one I can get to work is 127.0.0.1 which is the localhost address? I have already tried with no luck I want to use a unused one on my network.
Here's the code:
import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
if sys.argv[1:]:
port = int(sys.argv[1])
else:
port = 8000
server_address = ('127.0.0.1', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print "Running server on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
You can only use addresses that are bound to a network interface on the computer. You cannot use random addresses picked out of thin air.