Python - AttributeError: 'tuple' object has no attribute 'read' - python

I am working on a simple python client and server that can write code to a file as its sent. So far I have been stuck on this error: AttributeError: 'tuple' object has no attribute 'read'
Here is the client's code:
# CCSP Client
# (C) Chris Dorman - 2013 - GPLv2
import socket
import sys
# Some settings
host = raw_input('Enter the Host: ')
port = 7700
buff = 24
connectionmax = 10
# Connect to server
server = socket.socket()
server.connect((host, port))
print 'Connected!'
while True:
open_file = raw_input("File (include path): ")
fcode = open(open_file, "rb")
while True:
readcode = fcode.read(buff)
server.send(readcode)
if not fcode:
server.send("OK\n")
print "Transfer complete"
break
Server:
# CCSP Server
# (C) Chris Dorman - 2013 - GPLv2
import socket
import sys
import string
import random
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
host = "0.0.0.0"
port = 7700
buff = 1024
filepath = "/home/chris/"
extension = ".txt"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
print "Server Started"
while True:
server.listen(1)
conn = server.accept()
print 'Client' + str(conn)
print 'Generating a random file'
filename = filepath + str(id_generator()) + extension
fcode = open(filename, "wb")
while True:
if conn != 0:
code = conn.read(buff)
fcode.write(buff)
if conn == "DONE":
print 'Transfer complete'
break #EOT
Any help with getting this to work would be awesome. I just keep getting that dumb error when it gets down to: code = conn.read(buff) on the servers script

You should read some doc. accept() returns a tuple not a file-like object.

As others have pointed out, accept() returns a tuple. It looks like you want the first item in the tuple, which will be a new socket object.
Of course, sockets don't have a read() method either. I'm guessing that what you actually want is:
code = conn.recv(buff)
As recv() returns data that has been written to a socket's connection.

Related

AttributeError in python code with socket

I'm having an AttributeError that says "AttributeError: 'socket' object has no attribute 'upper'". I'm pretty sure I created and established the connection properly, and am still unsure what to do after consulting the socket documentation.
Thank you.
import socket
from _thread import *
import threading
print_lock = threading.Lock()
list_of_clients = []
def threaded(data, addr, s):
s.sendto(data.upper(), addr)
while True:
message = s.recv(1024)
if not message:
print('Bye')
print_lock.release()
break
message = message.upper()
print("Sending message to " + addr[0])
s.sendto(message, addr)
data.close()
def Main():
list_of_clients = []
serverName = 'localhost'
serverPort = 12000
with socket.socket(socket.AF_INET , socket.SOCK_STREAM) as serverSocket:
serverSocket.connect((serverName, serverPort))
while True :
print('Ready to ping...')
data, addr = serverSocket.accept()
print(type(data))
print_lock.acquire()
print("Client connected ip:<" + str(addr) + ">")
start_new_thread(threaded, (data, addr, serverSocket))
print("Continue")
if __name__ == '__main__':
Main()
The problem is this line:
s.sendto(data.upper(), addr)
data is defined here:
data, addr = serverSocket.accept()
The accept method returns a 2-tuple, where the first element is a socket object capable of sending and receiving data.
Therefore, instead of passing serverSocket to threaded, you should pass the first element of the 2-tuple, as well as whatever data you want to send.

Napster-style peer-to-peer (P2P) file sharing system using rpyc and message-orianted(Python)

I have a task in which I should make Napster-style peer-to-peer (P2P) file sharing system. I used rpyc and message-oriented at the same time, but I have a problem when I download a file from other peer - the code just runs infinite and never stops, no output.
Peer has two classes Client and server
from socket import *
import socket
import os
import pickle
import rpyc
from rpyc.utils.server import ThreadedServer
from const import *
class Client():
conn = rpyc.connect(HOST, PORT) # Connect to the index_server
def lookUp(self,filename):
PeerList = self.conn.root.exposed_search(filename)
if PeerList==False:
print "no File with this Name"
else:
print PeerList
def register_on_server(self,Filename,port):
self.conn.root.exposed_register(Filename,port)
def download(self, serverhost, serverport, filename): # function download a file from another peer
sock.connect((serverhost,serverport))
print("Client Connected to download a file")
sock.send(pickle.dumps(filename))
localpath = "C:\Users\aa\PycharmProjects\task1\downloadfiles"
data = sock.recv(1024)
totalRecv = len(data)
f = open(localpath + '/' + filename, 'wb')
f.write(data)
filesize = os.path.getsize('C:\Users\aa\PycharmProjects\task1\uploadfiles' + '/' + filename)
while totalRecv < filesize:
data = sock.recv(1024)
totalRecv += len(data)
f.write(data)
print("File is downloaded Successfully")
sock.close()
class Server(rpyc.Service):
def __init__(self, host, port):
self.host = host
self.port = port # the port it will listen to
global sock
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # socket for incoming calls
sock.bind((self.host, self.port)) # bind socket to an address
sock.listen(5) # max num connections
def obtain(self):
remotepath = "C:\Users\aa\PycharmProjects\task1\uploadfiles"
while True:
client, address = sock.accept()
print("Client Connected to download a file")
try:
filename = client.recv(1024)
if os.path.exists(remotepath + '/' + filename):
filesize = os.path.getsize(remotepath + '/' + filename)
if filesize > 0:
client.send(str(filesize))
with open(remotepath + '/' + filename, 'rb') as f:
bytes = f.read(1024)
client.send(bytes)
while bytes != "":
bytes = f.read(1024)
client.send(bytes)
else:
client.send("Empty")
else:
client.send("False")
except:
client.close()
return False
if __name__ == "__Server__":
server = ThreadedServer(Server, hostname=Server.host, port=Server.port)
server.start()
{Peer2}
from time import sleep
import rpyc
from peer import *
from const import *
peer2 = Client()
print ('1-register')
print ('2-search')
print ('3-download')
while(True):
commend = raw_input("enter your commend")
if commend == 'register':
filename = raw_input("write the file name")
peer2.register_on_server(filename,PeeR2PORT)
elif commend == 'search':
filename = raw_input("write the file name")
peer2.lookUp(filename)
elif commend == 'download':
port = raw_input("enter the other peer port")
host = raw_input("enter the other peer host")
filename = raw_input("enter the file name")
peer1 = Server(PeeR1HOST, PeeR1PORT)
peer1.obtain()
peer2.download(host, port, filename)
You create a call to peer1.obtain() which runs the peer to accept calls from different peers to download the file. However, you try to call peer1.download() from the same peer while it is already listening for incoming calls. You need to separate peer1.download() to run from different peer.
You need to revise how Napster FileSharing System works.
We are not here to solve your assignment. You seem to have good knowledge with python, the issue is that you do not understand the task good enough. We can help you with understanding its concept, helping you with syntax errors,..,etc.

Python Networking responding wtih 'b'

I've just started python networking, and after looking at a few internet tutorials, I gave it a go... only problem is, whenever I get a response from the sever, it prints as in:
Recieved from: (Host & Port)b'Hey' - where I haven't put the b anywhere.
Here is the server code:
import socket
import tkinter
import time
import sys
def Main():
top = tkinter.Tk()
top.configure(background='black')
host = '10.0.0.2'
port = 5000
s = socket.socket()
s.bind((host, port))
s.listen(1)
c, addr = s.accept()
while True:
con = tkinter.Label(top, text="Connection from: " + str(addr), bg='red', fg='white').pack()
data = c.recv(1024)
if not data:
break
conn = tkinter.Label(top, text="Recieved from: " + str(addr) + str(data), bg='black', fg='white').pack()
top.mainloop()
c.close()
Main()
And my client:
import socket
def Main():
host = '10.0.0.2'
port = 5000
s = socket.socket()
s.connect((host, port))
message = input("> ")
while message != 'quit':
s.send(message.encode('ascii'))
message = input(">")
s.close()
Main()
Thanks for any input - I'm not really good at this yet! (My hosts aren't my computer so that's not the issue)
When you call socket.recv() in Python 3 it returns a bytes object, not a normal string. You can convert it to a normal string as follows:
data.decode('utf-8')

Sending, receiving with python socket

I'm currently trying to write process that embeds a sequence of n IPs into packets and send it off to n server. Each server remove the outermost IP and then forward it to said IP. This is exactly like tunneling I know. During the process I also want the server to do a traceroute to where it's forwarding the packet and send that back to the previous server.
My code currently will forward the packets but it's stuck on performing the traceroute and getting it. I believe it's currently stuck in the while loop in the intermediate server. I think it's having something to do with me not closing the sockets properly. Any suggestion?
Client
#!/usr/bin/env python
import socket # Import socket module
import sys
import os
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 17353 # Reserve a port
FILE = raw_input("Enter filename: \n ")
NIP = raw_input("Enter Number of IPs: ")
accepted_IP = 0
IP= []
while accepted_IP < int(NIP):
IP.append(raw_input("Enter destination IP: \n"))
accepted_IP +=1
#cIP = raw_input("Enter intemediate IP: \n ")
ipv = raw_input("Enter IP version... 4/6")
try:
s.connect((host, port))
print "Connection sucessful!"
except socket.error as err:
print "Connection failed. Error: %s" %err
quit()
raw = open(FILE,"rb")
size = os.stat(FILE).st_size
ls = ""
buf = 0
for i in IP:
while len(i) < 15:
i += "$"
ls += i
header = ipv+NIP+ls+FILE
print ls
s.sendall(header + "\n")
print "Sent header"
data = raw.read(56) +ipv + NIP + ls
print "Begin sending file"
while buf <= size:
s.send(data)
print data
buf += 56
data = raw.read(56) + ipv + NIP + ls
raw.close()
print "Begin receiving traceroute"
with open("trace_log.txt","w") as tracert:
trace = s.recv(1024)
while trace:
treacert.write(trace)
if not trace: break
trace = s.recv(1024)
print "finished forwarding"
s.close()
Intermediate server
#!/usr/bin/env python
import socket
import subprocess
srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
srvsock.bind( (socket.gethostname(), 17353) )
srvsock.listen( 5 ) # Begin listening with backlog of 5
# Run server
while True:
clisock, (remhost, remport) = srvsock.accept() #Accept connection
print
d = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
header = ""
while True:
b = clisock.recv(1)
if b == "\n":
break
header += b
num = 15 * int(header[1]) + 2
file_name = header[num:]
nheader = header[0]+ str(int(header[1])-1) + header[17:]
d.connect((socket.gethostname(), 12355))
d.sendall(nheader+'\n')
print "begin forwarding"
while True:
raw = clisock.recv(56 + num) # recieve data
ip = raw[-15:] # extract IP
ipv, NIP = raw[57] , str(int(raw[57])-1)
if NIP == "0":
while (raw):
print "stuck in this loop"
d.send(raw[:56])
raw=clisock.recv(56+num)
if not raw: break
else:
while (raw):
print raw[:57] + NIP + raw[59:-15]
print "\n"
d.send(raw[:57] + NIP + raw[59:-15])
raw = clisock.recv(56+num)
if not raw :break
print "Finish forwarding"
d.close()
break
print "Begin traceroute"
tracrt = subprocess.Popen(['traceroute','google.com'], stdout=subprocess.PIPE)
s.sendall(tracrt.communicate()[0])
print "Finished"
clisock.close()
s.close()
Destination server
import socket
s = socket.socket()
host = socket.gethostname()
port = 12355
s.bind((host,port))
s.listen(5)
while True:
csock, (client, cport) = s.accept()
print client
header = ""
while True:
b = csock.recv(1)
if b == "\n":
break
header += b
file_name = header[2:]
r = open("File_test_"+file_name,"wb")
print 'Opening file for writing'
while True:
print "Begin writing file" + " " + file_name
raw = csock.recv(56)
while (raw):
print raw
r.write(raw)
raw = csock.recv(56)
r.flush()
r.close()
print "finish writing"
break
print "closing connection"
csock.close()
s.close()
The intermediate server is stuck in clisock.recv() in this loop because the break condition not raw isn't met before the connection is closed by the client, and the client doesn't close the connection before receiving the traceroute from the intermediate server, so they are waiting on each other.
To remedy this, you might consider sending the file size to the intermediate server, so that it can be used to determine when the receive loop is done. Or, if your platform supports shutting down one half of the connection, you can use
s.shutdown(socket.SHUT_WR)
in the client after sending the file.

python search string from socket

I have the following text file:
ADDRESS1 192.168.124.1
ADDRESS2 192.168.124.2
ADDRESS3 192.168.124.3
And I wrote the following string server in python (strsrv.py) :
#!/usr/bin/env python
import socket
import sys
host = ''
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(backlog)
while 1:
global f
client, address = s.accept()
data = client.recv(size)
with open('list.txt', 'r') as my_file:
for f in my_file.readlines():
if(f.find('%s' % data)>-1):
data = f
if f:
client.send(data)
client.send(f)
client.close()
I'm trying to connect to this server sending a string. This string must match one of lines described on text file. Ex: sending 'ADDRESS1' should return 'ADDRESS1 192.168.124.1' from the server, but it doesn't works. Any string sent returns only the last line of the text file. Please could someone point me to the right direction? Thanks :)
How are you testing this? Assuming you open a socket and connect to the host you should see that you are in fact receiving the correct line as well as the last one. Why? Because in the for loop you keep changing the value of f, the last value of f will be the last line in the file, and you send it back after sending data (which at that point is the correct value).
Here's a suggestion for how you might modify your code (assuming you want the full line back and you dont want wildcarding):
#!/usr/bin/env python
import socket
import sys
host = ''
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(backlog)
# build dict of addresses first, no need to do this for each message
with open('list.txt', 'r') as my_file:
address_map = dict(line.split() for line in my_file)
while True:
client, address = s.accept()
req_address = client.recv(size)
ip = address_map.get(req_address, 'Not found')
client.send(req_address + ' ' + ip)
client.close()
You can simply test this by doing this while the above is running:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('', 50000))
s.send('ADDRESS2')
s.recv(1024)

Categories