So, in my server side, I created a dict that takes records from a .txt file:
records = {}
with open("data.txt") as f:
for line in f:
(name1, age, location, number) = line.split("|")
records[str(name1)] = [age, location, number]
As of now, I am able to print this DB on the server side :
print("** Python DB contents **\n")
for keys in records.keys():
value_list = records[keys]
print(keys + "|" + value_list[0] + "|" + value_list[1] + "|" + value_list[2])
However, I want to send this to the client side, but nothing is working for me.
I have tried sending it to the client side by creating a list that contains the contents, then sending it to the client like so:
Server:
for keys in records.keys():
value_list = records[keys]
string = keys + "|" + value_list[0] + "|" + value_list[1] + "|" + value_list[2]
client.send(string.encode())
Client:
buffer = server.recv(1024).decode() # receive from server
print(f"Server: {buffer}") # print in server what was sent
Anyone know what exactly is wrong here? This is how I've been sending and receiving stuff my whole code but in this case it just does not work!
Related
Wrote a function that tries to reconnect to SSH when a disconnect happens. Basically expanded my existing function that simply saved the images, which works fine. The code runs but does not work to re-establish connectivity. Any help would be appreciated.
def get_image_id_and_upload_folder_of_images(db_name, table_name, selector_list,
condition_label, condition_val, img_address):
"""
get image id using project name from db
:param db_name: str - name of the data-base (usually 'server')
:param table_name: str - name of the table (usually 'images')
:param selector_list: list of str - list of selectors for the query (usually ["id", "path"])
:param condition_label: str - condition for the sql statement (like 'path')
:param condition_val: list of str - value for the condition of the condition_label (like ['name_of_file.png'])
:param img_address: str - address of the images to send them to the ftp server
:return: returns image or project id
"""
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
retry_interval = 1
retry_interval = float(retry_interval)
timeout = int(20)
timeout_start = time.time()
while time.time() < timeout_start + timeout:
time.sleep(retry_interval)
try:
db, cur = db_info(db_name)
cond_list = ["'" + str(x) + "'" for x in condition_val]
condition = ",".join(cond_list)
selector = ",".join(selector_list)
# make a query for sql
query_stmt = "SELECT " + selector + " FROM " + table_name + " WHERE `" + \
condition_label + "` IN (" + str(condition) + ");"
image_ids = get_image_ids(cur, db, query_stmt, condition_val)
for idx, ids in enumerate(image_ids):
print(ids)
save_img_new_server(img_address + '/' + condition_val[idx], str(ids))
save_img_new_server(img_address + '/' + condition_val[idx], str(ids), hst="site.com",
folder_path='images/')
except paramiko.ssh_exception.NoValidConnectionsError as e:
print('SSH transport is not ready...')
continue
# print(img_address + '/' + condition_val[idx], str(ids))
return image_ids
Your code never calls client.connect(). In fact it doesn't interact with any paramiko module at all inside the while loop.
So I have to send a specific message to a server (which already exists).
This request follows this format:
DO dirname number_of_files [file_size file_name file_data]*
so for example it can be something like this:
DO dir 2 1421 house.png [binary data I assume?] 1239 info.txt [more binary data?]
As you can see there can be as many files as you want.
So what I was doing is creating a string where I append all the information and at the end send it all together, but I'm stuck at the data part.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(ip, port)
request = "DO " + directory + " " + str(number_of_files)
for file_number in range(0,number_of_files):
data = open("./" + directory + "/" + files_info[file_number*2], 'rb').read()
client_request += " " + " ".join(files_info[file_number*2 : 1 + file_number*2]) + " " + data
s.send((request + "\n").encode())
This does not working because I'm trying to append the data which is binary to a string (TypeError: must be str, not bytes). I tried using .decode() before appending but I get an utf-8 decoder error (UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte). I tried with utf-16 and ascii and it didn't work either.
Should I be handling this in another way? I have done more requests like this but none of them included data from files, only numbers/strings so it was never a problem before.
I am trying to follow this format because I have all the code right now done like this (and these steps I showed don't all happen in the same functions) so changing the way I send the messages is not optimal
Here is my simple working code to get something close to what you want:
import socket
ip = '192.168.10.137'
port = 4043
directory = 'C:/123/'
#with open('1.dat', 'wb') as file:
# file.write(b'\x00\x01\x1a\xa1')
#with open('2.dat', 'wb') as file:
# file.write(b'\x00\x01\x00\x00')
file_info = ['1.dat', '2.dat']
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((ip, port))
sock.send(
'DO {} {}'.format(directory, len(file_info)).encode('UTF-8'))
for filename in file_info:
with open(filename, mode='rb') as file:
data = file.read()
sock.send(
' {} '.format(len(data)).encode('UTF-8'))
sock.send(
'{} '.format(filename).encode('UTF-8'))
sock.send(data)
Data received by server (bytes):
b'DO C:/123/ 2 4 1.dat \x00\x01\x1a\xa1 4 2.dat \x00\x01\x00\x00'
I'm trying to write a message to a hardware mailbox using a memory mapped dev/mem/ but everything I send, gets sent twice. So in the code below, after sending a single 1 to MBOX, the mailbox depth indicates a depth of two.
When I read from the mailbox it has 2 ones. Any idea why this is happening?
with open("/dev/mem", "r+b") as mem_fd:
addr_p = mmap.mmap(mem_fd.fileno(), mem_size[UPROC_PIF_AREA], offset = mem_offsets[PROC_PIF_AREA])
#Sending the message header to register mbox_xt first
print "Sending message header"
packed_mbox_status = addr_p[MBOX_STATUS:MBOX_STATUS + 4]
mbox_to_microp_depth = struct.unpack("BBBB", packed_mbox_status)
print "mbox_to_microp_depth : " + str(mbox_to_microp_depth)
addr_p[MBOX_XT:MBOX_XT+4] = struct.pack("<L", 1)
packed_mbox_status = addr_p[MBOX_STATUS:MBOX_STATUS + 4]
mbox_to_microp_depth = struct.unpack("BBBB", packed_mbox_status)
print "mbox_to_microp_depth : " + str(mbox_to_microp_depth)
I'm using python version 2.7.9 and i try to send png file.
But something strange happens..i using sockets and sends a post request(or kind of).
I send the request to the server from the client,then i prints the length of the request received on the server, for example, the length is:1051.
Then I do a regex to take the png file data, and then prints the length, and the length is 2632, that he larger than the response?!
I think the problem is that it's actually write the content, but not the right of representation, I tried different things but they did not work, so I ask here how to solve this problem.
Server source code:
import socket
import re
server = socket.socket()
server.bind(('0.0.0.0',8080))
while True:
server.listen(2)
(client, client_addr) = server.accept()
print 'IP :',client_addr
res = client.recv(0xfffffff)
print len(res)
#get file name
file_name = res.split('&')[0]
file_name = str(file_name.split('=')[1])
print repr(res)
#get the data of the file
raw_img = str(re.findall("&photo_data=(.*)" ,res ,re.DOTALL))
print "File name:" + file_name
print "Size:" + str(len(raw_img))
with open(file_name, 'wb') as f:
f.write(raw_img)
print "Done"
Client source code:
import socket
client = socket.socket()
client.connect(('127.0.0.1',8080))
raw_data = open('test.png', 'rb').read()
save_file_name = raw_input("Enter the file name:")
print len(raw_data)
output = 'POST /upload HTTP/1.1\r\n'
output += 'Content-Length:' + str(len(raw_data)) + str(len(save_file_name)) + '\r\n\r\n'
output += 'file_name=' + save_file_name + '&'
output += 'photo_data=' + raw_data
print len(output)
client.send(output)
client.close()
First, you should use while True to receive the full data:
res = ''
while True:
data = client.recv(1024)
if not data:
break
res += data
print len(res)
Then, re.findall actually returns an array, not a string. So you should do this:
r = re.findall("&photo_data=(.*)" ,res ,re.DOTALL)
raw_img = str(r[0])
Now it works fine.
Why doesn't the code before work? Let's say we have a list:
r = ['\x45']
The data in raw_img part is basically like this. If we brutely convert this list to a str, we have:
print len(str[r])) # ['E'], 5
Actually, what we need is r[0]:
print len(str[r[0])) # 1
That's why the size of the file became larger.
I have been working on a TwitchTV Python chat bot for a while now, but I'm still getting to grips with Python.
It may seem simple, but this has confused me so I decided I would ask:
I'm currently pulling messages from Twitch Chat using data = irc.recv
What I want to do is use the data pulled and turn it into a string, so that I can then check for capitals in the messages using str.isupper()
I've already tried a few ways;
data = irc.recv (4096)
msg = data()
capsTime = "30s"
str = msg
if str.isupper():
message("[-] Woah! Hold back on the caps! (Timeout " + capsTime + ")")
message("/timeout " + user + capsTime)
# variable "user" already defined
This is just one, that unfortunately didn't work.
EDIT:
This is my new code, It runs without error messages, but It doesn't function as I want it to;
while True:
data = irc.recv (4096)
user = data.split(':')[1]
user = user.split('!')[0]
caps = data.split(':')[0]
capsmsg = str(data)
print data
if data.find('PING') != -1:
irc.send('PONG ' + data.split()[1] + '\r\n')
if capsmsg.isupper():
message("[-] Woah! Hold back on the caps, " + user + "! (Timeout 30s)")
message("/timeout " + user + " 30s")
EDIT 2:
Expected output:
If a message is found in ALL caps, it will print this message and time the user out:
message("[-] Woah! Hold back on the caps, " + user + "! (Timeout 30s)")
Current output:
The bot does not pick the message up or run the scripted code.
Try this:
data = irc.recv (4096)
# msg = data()
capsTime = "30s"
mystr = repr(data)
if mystr.isupper():
message("[-] Woah! Hold back on the caps! (Timeout " + capsTime + ")")
message("/timeout " + user + capsTime)
# variable "user" already defined
Don't use reserved keyword.