So I have this server file which is ok, always is up for connections, after sending a message from the client, client connection closes. I want the client to be up again for sending a message (get input again). I don't want to start the client again (in my case it's odev1.py)
# server.py
import socket
HOST = socket.gethostname()
PORT = 65432
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
server_socket.bind((HOST, PORT))
server_socket.listen()
while True:
print('waiting for a connection')
connection, client_address = server_socket.accept()
try:
print(f'{client_address[0]} has entered the chat\n')
while True:
data = connection.recv(1024)
print(f'{client_address[0]} - {str(data, encoding="utf-8")}')
if data:
print('sending data back to the client')
connection.sendall(data)
else:
print('no data from', client_address[0])
break
finally:
print("Closing current connection")
connection.close()
this is my client ->
# client.py
import sys
import socket
host = socket.gethostname()
port = 65432
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as e:
print("Error creating socket: %s" % e)
sys.exit(1)
# Second try-except block -- connect to given host/port
try:
s.connect((host, port))
except socket.gaierror as e:
print("Address-related error connecting to server: %s" % e)
sys.exit(1)
except socket.error as e:
print("Connection error: %s" % e)
sys.exit(1)
# Third try-except block -- sending data
try:
# NOTE: I want it to take input again
# Send data
message = input('write something: ')
s.sendall(message.encode('utf-8'))
print('sending {!r}'.format(message))
# Look for the response
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = s.recv(1024)
amount_received += len(data)
print('received {!r}'.format(data))
finally:
print('closing socket')
s.close()
this is how it looks right now:
Related
I'm trying to send a large file (.avi) over socket by sending the content of the file in chunks (a little bit like torrents). The problem is that the script doesn't send the file. I'm out of ideas here.
Any help or twerking of the script would be very appreciated.
Server:
import socket
HOST = ""
PORT = 8050
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(1)
conn, addr = sock.accept()
print("Connected by ", str(addr))
while 1:
data = conn.recv(1024)
if data.decode("utf-8") == 'GET':
with open(downFile,'rb') as output:
l = output.read(1024)
while (l):
conn.send(l)
l = output.read(1024)
output.close()
conn.close()
Client:
import socket
HOST = "localhost"
PORT = 8050
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST,PORT))
while 1:
message = input()
sock.send(bytes(message,'UTF-8'))
conn.send(str.encode('GET'))
with open(downFile, 'wb+') as output:
while True:
rec = str(sock.recv(1024), "utf-8")
if not rec:
break
output.write(rec)
output.close()
print('Success!')
sock.close()
Here are a working client and server that should demonstrate transferring a file over a socket. I made some assumptions about what your code was supposed to do, for example, I assumed that the initial message the client sent to the server was supposed to be the name of the file to download.
The code also includes some additional functionality for the server to return an error message to the client. Before running the code, make sure the directory specified by DOWNLOAD_DIR exists.
Client:
import socket
import sys
import os
HOST = "localhost"
PORT = 8050
BUF_SIZE = 4096
DOWNLOAD_DIR = "downloads"
def download_file(s, down_file):
s.send(str.encode("GET\n" + down_file))
rec = s.recv(BUF_SIZE)
if not rec:
return "server closed connection"
if rec[:2].decode("utf-8") != 'OK':
return "server error: " + rec.decode("utf-8")
rec = rec[:2]
if DOWNLOAD_DIR:
down_file = os.path.join(DOWNLOAD_DIR, down_file)
with open(down_file, 'wb') as output:
if rec:
output.write(rec)
while True:
rec = s.recv(BUF_SIZE)
if not rec:
break
output.write(rec)
print('Success!')
return None
if DOWNLOAD_DIR and not os.path.isdir(DOWNLOAD_DIR):
print('no such directory "%s"' % (DOWNLOAD_DIR,), file=sys.stderr)
sys.exit(1)
while 1:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((HOST, PORT))
except Exception as e:
print("cannot connect to server:", e, file=sys.stderr)
break
file_name = input("\nFile to get: ")
if not file_name:
sock.close()
break
err = download_file(sock, file_name)
if err:
print(err, file=sys.stderr)
sock.close()
Server:
import socket
import sys
import os
HOST = ""
PORT = 8050
BUF_SIZE = 4096
def recv_dl_file(conn):
data = conn.recv(1024)
if not data:
print("Client finished")
return None, None
# Get command and filename
try:
cmd, down_file = data.decode("utf-8").split("\n")
except:
return None, "cannot parse client request"
if cmd != 'GET':
return None, "unknown command: " + cmd
print(cmd, down_file)
if not os.path.isfile(down_file):
return None, 'no such file "%s"'%(down_file,)
return down_file, None
def send_file(conn):
down_file, err = recv_dl_file(conn)
if err:
print(err, file=sys.stderr)
conn.send(bytes(err, 'utf-8'))
return True
if not down_file:
return False # client all done
# Tell client it is OK to receive file
sent = conn.send(bytes('OK', 'utf-8'))
total_sent = 0
with open(down_file,'rb') as output:
while True:
data = output.read(BUF_SIZE)
if not data:
break
conn.sendall(data)
total_sent += len(data)
print("finished sending", total_sent, "bytes")
return True
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(1)
keep_going = 1
while keep_going:
conn, addr = sock.accept()
print("Connected by", str(addr))
keep_going = send_file(conn)
conn.close() # close clien connection
print()
sock.close() # close listener
I created a HTTP proxy using this tutorial https://null-byte.wonderhowto.com/how-to/sploit-make-proxy-server-python-0161232/.
I am using this proxy with firefox browser. When I open a website in browser. The connection between firefox-proxy and proxy-webserver is successful and the proxy successfully receives data from webserver. But when I sent the data back to browser it doesn't render any page (I don't see webpage in browser). What may be the issue here ?
import socket, sys
from thread import *
try:
listening_port = int(raw_input("[*] Enter Listening Port Number: "))
except KeyboardInterrupt:
print "\n[*] User Requested An Interrupt"
print "[*] Application Exiting ..."
sys.exit()
max_conn = 20 #Max Connections in Queue
buffer_size = 262144 # Max socket Buffer size
def start():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # initiates socket
s.bind(('', listening_port)) # Bind socket for listen
s.listen(max_conn) # start listening for incoming connections
print "[*] Initializing Sockets ... Done"
print "[*] Sockets Binded succesfully ..."
print("[*] Server started succesfully [ %d ]\n" % (listening_port))
except Exception, e:
print "[*] Unable to initialize socket", e
sys.exit(2)
while 1:
try:
conn, addr = s.accept() # Accept connection From client browser
print "Connection accepted from browser"
data = conn.recv(buffer_size) # Receive CLient Data
print "Received request from browser"
start_new_thread(conn_string, (conn, data, addr)) # start a thread
except KeyboardInterrupt:
print "\n[*] Proxy Server Shutting down ..."
sys.exit(1)
s.close()
def conn_string(conn, data, addr):
# Client Browser Request Appears Here
try:
first_line = data.split('\n')[0]
url = first_line.split(' ')[1]
print "URL ", url
http_pos = url.find("://") # find the position of ://
if (http_pos == -1):
temp = url
else:
temp = url[(http_pos+3):] # get the rest of the URL
print "http pos, Temp", http_pos, temp
port_pos = temp.find(":") # Find the postion of port (if any)
webserver_pos = temp.find("/") # Find the end of the web server
if webserver_pos == -1:
webserver_pos = len(temp)
webserver = ""
port = -1
#print "Port pos, webserver_pos", port_pos, webserver_pos
if (port_pos == -1 or webserver_pos < port_pos): # default port
port = 80
webserver = temp[:webserver_pos]
else:
# Specific port
port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
webserver = temp[:port_pos]
# print "WEB server", webserver
print "Data extracted from request Request"
proxy_server(webserver, port, conn, addr, data)
except Exception, e:
pass
def proxy_server(webserver, port, conn, addr, data):
try:
print "starting connection towebserver"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#print "connecing host", webserver, port
s.connect((webserver, port))
print "connected"
#print "data", data
s.send(data)
print "Request sent"
while 1:
# Read reply or data to from end web server
reply = s.recv(buffer_size)
print "Reply Received ", reply
if (len(reply) > 0):
conn.send(reply) # send reply back to client
# Send notification to proxy Server [script itself]
dar = float(len(reply))
dar = float(dar / 1024)
dar = "%.3s" % (str(dar))
dar = "%s KB" % (dar)
print "[*] Request Done: %s => %s <=" % (str(addr[0]), str(dar))
else:
break
s.close()
conn.close()
print "Conn CLosed ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
except socket.error, (value, message):
print "Error", message
s.close()
conn.close()
sys.exit(1)
start()
Just started learning python socket programming. I'm trying to send and receive data from a Raspberry Pi 3 using python sockets. I'm running the server program on the Raspberry Pi and the client program on my laptop running Ubuntu. Both are on the same WiFi network. The program runs and the client connects to the server, but there is no data transfer. Both the client and server do nothing. The two programs run properly if I try to run them both on the same device. Here's my code :-
Client
import socket
HOST = '172.16.31.51'
PORT = 5000
BUFSIZ = 1024
if __name__ == '__main__':
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = input("Enter hostname [%s]: " %HOST) or HOST
port = input("Enter port [%s]: " %PORT) or PORT
sock_addr = (host, int(port))
client_sock.connect(sock_addr)
payload = 'GET TIME'
try:
while True:
client_sock.send(payload.encode('utf-8'))
data = client_sock.recv(BUFSIZ)
print(repr(data))
more = input("Want to send more data to server[y/n]:")
if more.lower() == 'y':
payload = input("Enter payload: ")
else:
break
except KeyboardInterrupt:
print("Exited by user")
client_sock.close()
Server
import socket
from time import ctime
PORT = 5000
BUFSIZ = 1024
ADDR = ('', PORT)
if __name__ == '__main__':
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.bind(ADDR)
server_socket.listen(5)
server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
while True:
print('Server waiting for connection...')
client_sock, addr = server_socket.accept()
print('Client connected from: ', addr)
while True:
data = client_sock.recv(BUFSIZ)
if not data or data.decode('utf-8') == 'END':
break
print("Received from client: %s" % data.decode('utf-8'))
print("Sending the server time to client: %s" %ctime())
try:
client_sock.send(bytes(ctime(), 'utf-8'))
except KeyboardInterrupt:
print("Exited by user")
client_sock.close()
server_socket.close()
EDIT:
The screenshots - https://imgur.com/a/NgzsC
As soon as I hit Ctrl + C, on the client side, the server seems to send some data before it disconnects from the client. Here's a screenshot of that - https://imgur.com/a/hoLwN
Your sockets work OK, but you made some other programming mistakes. Did you ever run it?
This is the modified working code.
(Hint: you can start testing just the client when you use nc -l 5000 as a simple manual server.
Client
import socket
HOST = '127.0.0.1'
PORT = 5000
BUFSIZ = 1024
if __name__ == '__main__':
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = str(raw_input("Enter hostname [%s]: " %HOST)) or HOST
port = str(raw_input("Enter port [%s]: " %PORT)) or PORT
sock_addr = (host, int(port))
client_sock.connect(sock_addr)
payload = 'GET TIME'
try:
while True:
client_sock.send(payload.encode('utf-8'))
data = client_sock.recv(BUFSIZ)
print(repr(data))
more = raw_input("Want to send more data to server[y/n]:")
if more.lower() == 'y':
payload = str(raw_input("Enter payload: "))
else:
break
except KeyboardInterrupt:
print("Exited by user")
client_sock.close()
Server
import socket
from time import ctime
PORT = 5004
BUFSIZ = 1024
ADDR = ('', PORT)
if __name__ == '__main__':
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.bind(ADDR)
server_socket.listen(5)
server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
while True:
print('Server waiting for connection...')
client_sock, addr = server_socket.accept()
print('Client connected from: ', addr)
while True:
data = client_sock.recv(BUFSIZ)
if not data or data.decode('utf-8') == 'END':
break
print("Received from client: %s" % data.decode('utf-8'))
print("Sending the server time to client: %s" %ctime())
try:
client_sock.send( ctime().encode('utf-8') )
except KeyboardInterrupt:
print("Exited by user")
break;
client_sock.close()
server_socket.close()
So I found this chat program on http://www.bogotobogo.com/python/python_network_programming_tcp_server_client_chat_server_chat_client_select.php
And I want to use this at my school for me and my friends, put I don't want to chat to be ex: [192.168.1.3] "Message". I want to be able to add another argument so it shows there name they input instead of Ip. It would be a little hard to have everyone using ips. Any suggestions?
Server Code:
# chat_server.py
import sys
import socket
import select
HOST = ''
SOCKET_LIST = []
RECV_BUFFER = 4096
PORT = 9009
def chat_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
# add server socket object to the list of readable connections
SOCKET_LIST.append(server_socket)
print "Chat server started on port " + str(PORT)
while 1:
# get the list sockets which are ready to be read through select
# 4th arg, time_out = 0 : poll and never block
ready_to_read,ready_to_write,in_error = select.select(SOCKET_LIST,[],[],0)
for sock in ready_to_read:
# a new connection request recieved
if sock == server_socket:
sockfd, addr = server_socket.accept()
SOCKET_LIST.append(sockfd)
print "Client (%s, %s) connected" % addr
broadcast(server_socket, sockfd, "[%s:%s] entered our chatting room\n" % addr)
# a message from a client, not a new connection
else:
# process data recieved from client,
try:
# receiving data from the socket.
data = sock.recv(RECV_BUFFER)
if data:
# there is something in the socket
broadcast(server_socket, sock, "\r" + '[' + str(sock.getpeername()) + '] ' + data)
else:
# remove the socket that's broken
if sock in SOCKET_LIST:
SOCKET_LIST.remove(sock)
# at this stage, no data means probably the connection has been broken
broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
# exception
except:
broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
continue
server_socket.close()
# broadcast chat messages to all connected clients
def broadcast (server_socket, sock, message):
for socket in SOCKET_LIST:
# send the message only to peer
if socket != server_socket and socket != sock :
try :
socket.send(message)
except :
# broken socket connection
socket.close()
# broken socket, remove it
if socket in SOCKET_LIST:
SOCKET_LIST.remove(socket)
if __name__ == "__main__":
sys.exit(chat_server())
Client Code:
# chat_client.py
import sys
import socket
import select
def chat_client():
if(len(sys.argv) < 3) :
print 'Usage : python chat_client.py hostname port'
sys.exit()
host = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
# connect to remote host
try :
s.connect((host, port))
except :
print 'Unable to connect'
sys.exit()
print 'Connected to remote host. You can start sending messages'
sys.stdout.write('[Me] '); sys.stdout.flush()
while 1:
socket_list = [sys.stdin, s]
# Get the list sockets which are readable
ready_to_read,ready_to_write,in_error = select.select(socket_list , [], [])
for sock in ready_to_read:
if sock == s:
# incoming message from remote server, s
data = sock.recv(4096)
if not data :
print '\nDisconnected from chat server'
sys.exit()
else :
#print data
sys.stdout.write(data)
sys.stdout.write('[Me] '); sys.stdout.flush()
else :
# user entered a message
msg = sys.stdin.readline()
s.send(msg)
sys.stdout.write('[Me] '); sys.stdout.flush()
if __name__ == "__main__":
sys.exit(chat_client())
How can I have a Python socket server tell me when there is a connection and then CONTINUE to give me back data that the client sends? When I do it, it connects and then just loops over, telling me it connected over and over again.
I,just want it to connect and then continually check (or grab) data sent to the server.
Also, how can I tell if the client disconnected?
address = ('', 7777)
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
try:
server_socket.bind(address)
except Exception, e:
print colored("Address already in use", 'red')
server_socket.listen(2)
print colored("Socket ready", 'blue')
while True:
client_socket, addr = server_socket.accept()
hostIP = addr[0]
port = addr[1]
try:
host = gethostbyaddr(hostIP)[0]
except:
host = hostIP
print colored("Got connection from: " + host, 'blue')
try:
recv_data = server_socket.recv(2048)
print("Got: " + recv_data)
except:
print "nothing"
recv_data = "" # this is because I test what it is later, but that's irrevlevant.
Thanks
You didn't do anything with client_socket; ie: the actual client connection. Furthermore, the server
cannot know how much the client wants to send and so it must CONTINUE (ie: in a
loop) to receive data. When the connection sends 'empty' data, the connection
is terminated and the server goes back to listening. If you want the server to
accept new connections and continue to receive data from existing connections
look up the threading module.
import socket
address = ('', 7777)
server_socket = socket.socket(AF_INET, SOCK_STREAM)
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
try:
server_socket.bind(address)
except Exception, e:
print colored("Address already in use", 'red')
server_socket.listen(2)
print colored("Socket ready", 'blue')
while True:
client_socket, addr = server_socket.accept()
hostIP = addr[0]
port = addr[1]
try:
host = gethostbyaddr(hostIP)[0]
except:
host = hostIP
print colored("Got connection from: " + host, 'blue')
while True:
try:
recv_data = client_socket.recv(2048)
if not recv_data:
break
print("Got: " + recv_data)
except socket.error, e:
print "nothing"
recv_data = "" # this is because I test what it is later, but that's irrevlevant.