Python socket.error: [Errno 32] Broken pipe - python

i made a python listener(server) on my vps
but when i give the server and client the ip addreess of vps and port 8585
this error shows:
error :
socket.error: [Errno 32] Broken pipe
i use python version 2 in vps
i use python version 3 in my PC
my server code :
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip = raw_input("ip : ")
ip = str(ip)
port = raw_input("port : ")
port = int(port)
s.bind((ip,port))
s.listen(5)
while True:
c, addr = s.accept()
s.send("welcome !")
print (addr, "connected.")`
client :
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST = input("HOST : ")
HOST = str(HOST)
PORT = input("PORT : ")
PORT = int(PORT)
s.connect((HOST,PORT))
buff = 1024
data = s.recv(buff)
print(data)`

In the server you have:
c, addr = s.accept()
s.send("welcome !")
You must do the send on the connected socket to the client and not on the listener socket, i.e. it should be c.send instead of s.send.

Related

socket question: ConnectionResetError: [Errno 54] Connection reset by peer

I am writing a simple socket app, and I have met this message and don't know how to fix it.
server_input =s.recv(1024)
ConnectionResetError: [Errno 54] Connection reset by peer
server.py
import socket
def main():
s = socket.socket()
port = 58257
client_address = "localhost"
s.bind((client_address, port))
s.listen(2)
user_input = input("Enter a message: ")
s.send(user_input.encode())
s.close()
if __name__ == '__main__':
main()
client.py
import socket
def main():
s = socket.socket()
#host = socket.gethostname()
port = 58257
client_address = "localhost"
s.connect((client_address, port))
print("connet ")
server_input =s.recv(1024)
server_input.decode()
print("Received message: " + server_input)
s.close()
if __name__ == '__main__':
main()
It is there any problem with my code? Thank you so much!!!
s.bind((client_address, port))
s.listen(2)
user_input = input("Enter a message: ")
s.send(user_input.encode())
The server needs to call accept on the server socket to get a socket for the connected client, then call send on this connected socket, not on the server socket.
Without this the connect in the client will still succeed, since it is done by the OS kernel on the server side. But since the connected socket is never accepted by the server it will be reset, thus resulting in the error you see.
So a more correct code on the server side would be
...
s.listen(2)
c,_ = s.accept()
user_input = input("Enter a message: ")
c.send(user_input.encode())
c.close()
Note that your client code has also other problems, i.e it should be
print("Received message: " + server_input.decode())

How to connect 2 computers on diffrent networks using python sockets

I'm trying to connect 2 different computers that on different networks,
but I got an error:
TimeoutError: [Errno 110] Connection timed out
And sometimes I got this error:
OSError: [Errno 113] No route to host
I wrote the server.py script and started it on the first pc and client.py on the second one.
server.py
import socket
server_socket = socket.socket()
server_socket.bind(("127.0.0.1", 80))
server_socket.listen(1)
(client_socket, client_address) = server_socket.accept()
print ("client_connected [" + client_address[0] + "]")
client_command = client_socket.recv(1024).decode()
print(client_command.encode())
client.py
import socket
client_socket = socket.socket()
client_socket.connect(("server_public_ip_here", 8820))
client_command = input("command: ")
client_socket.send(client_command.encode())
data = client_socket.recv(1024).decode()
print("server: " + data)
I expect to get a connections between the computers but keep getting a TimeoutError or OSError
if you are on two separate computers you should bind to 0.0.0.0 (to listen to all network connections) or to the server's IP for connections on that IP, and not 127.0.0.1 (localhost). Works if client and server are both in the same machine. Also you should use the same port on client and server.
I test it and it worked:
server.py
import socket
server_socket = socket.socket()
server_socket.bind(("0.0.0.0", 8000))
server_socket.listen(1)
(client_socket, client_address) = server_socket.accept()
print ("client_connected [" + client_address[0] + "]")
client_command = client_socket.recv(1024).decode()
print(client_command.encode())
client.py
import socket
client_socket = socket.socket()
client_socket.connect(("127.0.0.1", 8000)) #or enter ip of server
client_command = input("command: ")
client_socket.send(client_command.encode())
data = client_socket.recv(1024).decode()
print("server: " + data)

I'm having a problem with my Python socket program [WinError 10057]

I was programming a simple client-server socket program that worked on two different computers.
The server is a desktop with a static ip address, and the client is a laptop connected to a Wi-Fi. Both are using Windows 10 as operating system.
I also opened the firewall port.
Here is my code.
This code works well within one computer, but WinError 10057 occurs when another computer(my laptop) tries to connect to the server.
server.py
from socket import *
import sys
HOST = '0.0.0.0'
PORT = 16161
BUFSIZE = 1024
ADDR = (HOST, PORT)
CLIENT_NUM = 5
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(ADDR)
print('bind')
serverSocket.listen(CLIENT_NUM)
print('listen')
while True:
try:
connectionSocket, addr_info = serverSocket.accept()
print('accept')
print('--client information--')
print(connectionSocket)
data = connectionSocket.recv(BUFSIZE)
print('Received data:', data.decode())
connectionSocket.send('OK'.encode())
connectionSocket.close()
except KeyboardInterrupt:
sys.exit(0)
client.py
from socket import *
import sys
HOST = '*.*.*.*' # server's ip address
PORT = 16161
BUFSIZE = 1024
ADDR = (HOST, PORT)
clientSocket = socket(AF_INET, SOCK_STREAM)
try:
clientSocket.connect_ex(ADDR)
clientSocket.send('Hello!'.encode()) # WinError 10057 occurs
except Exception as e:
print(e)
print('%s:%s' % ADDR)
sys.exit(1)
print('connect is success')
receive = clientSocket.recv(BUFSIZE)
print(receive.decode())
clientSocket.close()
I've fixed it. I asked my organization to open the firewall ports, and the connection was successful when the firewall ports were opened.

OSError: [Errno 99] Cannot assign requested address - py

I just copied this code for a client/server example:
http://www.bogotobogo.com/python/python_network_programming_server_client.php
and when I run the code I get this error:
OSError: [Errno 99] Cannot assign requested address
Server:
# server.py
import socket
import time
# create a socket object
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = socket.gethostname()
port = 9999
# bind to the port
serversocket.bind((host, port))
# queue up to 5 requests
serversocket.listen(5)
while True:
# establish a connection
clientsocket,addr = serversocket.accept()
print("Got a connection from %s" % str(addr))
currentTime = time.ctime(time.time()) + "\r\n"
clientsocket.send(currentTime.encode('ascii'))
clientsocket.close()
Client:
# client.py
import socket
# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = socket.gethostname()
port = 9999
# connection to hostname on the port.
s.connect((host, port))
# Receive no more than 1024 bytes
tm = s.recv(1024)
s.close()
print("The time got from the server is %s" % tm.decode('ascii'))
Replace host = socket.gethostname() with host = '127.0.0.1' and it should work.
Check this
Hope this wil help :)

Socket.error: [Errno 10022] An invalid argument was supplied

#!/usr/bin/env python
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('192.168.1.123', 5162))
clientsocket.send('getval.1')
clientsocket.close
clientsocket.bind(('192.168.1.124', 5163))
clientsocket.listen(1)
while True:
connection, address=clientsocket.accept()
value=connection.recv(1024)
print value
I'm trying to get python to send a message to the server, and in return the server responds. Yet when I execute this code it gives me
Socket.error: [Errno 10022] An invalid argument was supplied
It seems you wrote a mixed code of server and client
Here a simple sample of codes for socket programming the first on server side and the second on client
Server side code:
# server.py
import socket
import time
# create a socket object
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = socket.gethostname()
port = 9999
# bind to the port
serversocket.bind((host, port))
# queue up to 5 requests
serversocket.listen(5)
while True:
# establish a connection
clientsocket,addr = serversocket.accept()
print("Got a connection from %s" % str(addr))
currentTime = time.ctime(time.time()) + "\r\n"
clientsocket.send(currentTime.encode('ascii'))
clientsocket.close()
and now the client
# client.py
import socket
# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = socket.gethostname()
port = 9999
# connection to hostname on the port.
s.connect((host, port))
# Receive no more than 1024 bytes
tm = s.recv(1024)
s.close()
print("The time got from the server is %s" % tm.decode('ascii'))
The server simply remained listened for any client and when it finds out a new connection it returns current datetime and closes the client connection

Categories