I'm a newbie, and this is my first question on stackoverflow, so with that said, here's my question
CLIENT CODE
import socket
cs = socket.socket()
ADDR = ('192.168.29.139',9999)
cs.connect(ADDR)
l = int(cs.recv(2048).decode())
data = cs.recv(l).decode()
data = eval(data)
cont = data["file"]["cont"]
f = open(data['file']['name'] + data['file']['ext'], "wb")
f.write(cont)
f.close()
SERVER CODE
## SERVER SIDE
import socket
ss = socket.socket()
ADDR = ('192.168.29.139',9999)
ss.bind(ADDR)
ss.listen()
conn, addr = ss.accept()
msg = input("Enter message: ")
filepath = input("Enter filepath: ")
fileName = input("Enter filename : ")
fileExt = input("Enter fileExt:" )
f = open(filepath,"rb")
r = f.read()
f.close()
fileDict = {"name": fileName, "ext": fileExt, "cont": r}
msg_dict = {"msg":msg, "file": fileDict}
msg_dict = str(msg_dict).encode()
conn.send(str(len(msg_dict)).encode())
conn.send(msg_dict)
This method works totally fine when I transfer files within the same computer (even bigger files, like several GBs, in this test I was using the windows 7 test video, that was about 25MB) but when i use it on LAN to transfer the same file, between two computers within the same network it shows an error
right during this statement
data = eval(data)
the error was, after printing so many lines of characters like \xo... and empty lines
EOL while scanning string literal ^
using pickle also gave error
Thank you for reading... Please HELP!
Related
I wrote some code to extract email and IP addresses from bulk text. However, the code extracts only the email addresses. (The original text, which I would like to make understandable, is a typical log file). I don't know why the generated file does not give me back the IP addresses.
import os
import re
# 1
filename = 'errors.txt'
newfilename = 'emaillist-rev.txt'
# 2
if os.path.exists(filename):
data = open(filename,'r')
bulkemails = data.read()
else:
print "File not found."
raise SystemExit
# 3
r = re.compile(r'[\w\.-]+#[\w\.-]+')
results = r.findall(bulkemails)
emails = ""
for x in results:
emails += str(x)+"\n"
# 4
ip = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
result = ip.findall(bulkemails)
ip =""
for y in result:
ip += str(y)+"\n"
# 5
def writefile():
f = open(newfilename, 'w')
f.write(emails + ip)
f.close()
print "File written."
# 6
def overwrite_ok():
response = raw_input("Are you sure you want to overwrite "+str(newfilename)+"? Yes or No\n")
if response == "Yes":
writefile()
elif response == "No":
print "Aborted."
else:
print "Please enter Yes or No."
overwrite_ok()
# 7
if os.path.exists(newfilename):
overwrite_ok()
else:
writefile()
When declaring the ip regex, replace the anchors with word boundaries and mind you need to use a raw string literal.
ip = re.compile(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b')
I am trying to create a function that allows the client to download a file from the server. Below oare the relevant portions of the code. I have gotten to the point where a text file with the same name is created in the client folder, but it is just empty with no data. The programs get to the "received file line" but nothin else.
Client Code:
elif cmd == "DOWN":
filename = data[1]
print(filename)
client.send(filename.encode(FORMAT))
f = open("client/" + filename, 'wb')
while True:
print("receiving data")
data = client.recv(1024).decode(FORMAT)
if not data:
break
f.write(data)
print("Data: %s", (data))
f.close()
Server Code:
elif cmd == "DOWN":
file = conn.recv(1024).decode(FORMAT)
f = open("server/"+file, 'rb')
l = f.read(1024)
while (l):
conn.send(l.encode(FORMAT))
print("send data", repr(1))
l = f.read(1024)
f.close()
print("done sending")
Output:
Welcome to the server
> DOWN abc.txt
abc.txt
recieving data
The program enters the loop to receive the data, but nothing else. Any help would be appreciated. Thank you.
I am still new to Python, and have been working on this for work, and a few side projects with it for automating my Plex Media Management tasks.
I am trying to write a python script that would allow me to take a set list of domains from a csv file, match them to their dns name: Example (Plex.tv using 'NS' would return jeremy.ns.cloudflare.com)
My main goal is to read in the list of domains from a csv
run my code to match those domains to a dns resolver name
write those to either a new CSV file, and then zip the two together, which is what I have in my code.
I am having a few problems along the way.
Visual Code doesn't allow import dns.resolver (not a huge issue, but if you know the fix for that it would save me from having to run it from command line)
Matching Domains to their DNS resolver is throwing the error "AttributeError: 'list' object has no attribute 'is_absolute'"
import csv
import socket
import dns.resolver
import os
from os.path import dirname, abspath
# Setting Variables
current_path = dirname(abspath(__file__))
domainFName = '{0}/domains.csv'.format(current_path)
outputFile = '{0}/output.csv'.format(current_path)
dnsList = '{0}/list2.csv'.format(current_path)
case_list = []
fields = ['Domains', 'DNS Resolvers']
caseList = []
dnsResolve = []
# Read in all domains from csv into list
with open(domainFName, 'r') as file:
for line in csv.reader(file):
case_list.append(line)
print(case_list)
# Match domains to the DNS Resolver Name
for domains in case_list:
answer = dns.resolver.resolve(domains, 'NS')
server = answer.target
dnsResolve.append(server)
# Write the dns Resolver names into a new csv file
with open(dnsList,'w', newline="") as r:
writers = csv.writer(r)
writers.writerows(caseList)
# Write the domains and dns resolvers to new output csv
with open(outputFile,'w', newline="") as f:
writer = csv.writer(f)
writer.writerow(fields)
writer.writerow(zip(case_list,caseList))
exit()
Thanks for any help
After a discussion with a co-worker, I was able to resolve my issue, and just for the sake of it, if anyone wants to use this code for a similar need (we use it for DMARC), I will post the whole code:
import dns.resolver
import csv
import os
from os.path import dirname, abspath
# Setting Variables
current_path = dirname(abspath(__file__))
domainFName = '{0}/domains.csv'.format(current_path)
outputFile = '{0}/output.csv'.format(current_path)
dnsList = '{0}/dnslist.csv'.format(current_path)
backupCSV = '{0}/backup-output.csv'.format(current_path)
case_list = []
dns_list = []
fields = ['Domains', 'DNS Resolvers']
csv_output = zip(case_list, dns_list)
domainAmount = 0
rd = 00
dnresolve = 00
part = 0
percentL = []
percents = [10,20,30,40,50,60,70,80,90,95,96,97,98,99]
percentList = []
floatingList = []
floatPart = []
x = 00
keyAzure = 'azure'
keyCSC = 'csc'
while x < .99:
x += .01
floatingList.append(x)
# THIS IS THE CODE FOR WRITING CSV FILES INTO LISTS - LABELED AS #1
print("FILES MUST BE CSV, WILL RETURN AN ERROR IF NOT. LEAVE OFF .CSV")
# Here we will gather the input of which csv file to use. If none are entered, it will use domains.csv
print("Enter your output file name (if blank will use default):")
UserFile = str(input("Enter your filename: ") or "domains")
fullFile = UserFile + '.csv'
domainFName = fullFile.format(current_path)
# Here will will specify the output file name. If the file is not created, it will create it
# If the user enters not data, the default will be used, output.csv
print("Enter your output file name (if blank will use default):")
UserOutput = str(input("Enter your filename: ") or "output")
fullOutput = UserOutput + '.csv'
outputFIle = fullOutput.format(current_path)
# Read in all domains from csv into list
with open(domainFName, 'r') as file:
for line in csv.reader(file):
case_list.append(line)
domainAmount += 1
print("Starting the resolver:")
print("You have " + str(domainAmount) + " Domains to resolve:")
# THIS IS THE END OF THE CODE FOR WRITING CSV FILES INTO LISTS - LABELED AS #1
# THE CODE BELOW IS WORKING FOR FINDING THE DNS RESOLVERS - LABELED AS #2
# Function for matching domains to DNS resolvers
def dnsResolver (domain):
try:
answers = dns.resolver.resolve(domain, 'NS')
for server in answers:
dns_list.append(server.target)
except:
dns_list.append("Did Not Resolve")
print("Now resolving domains to their DNS name:")
print("This will take a few minutes. Check out the progress bar for your status:")
print("I have resolved 0% Domains:")
# This code is for finding the percentages for the total amount of domains to find progress status
def percentageFinder(percent, whole):
return (percent * whole) / 100
def percentGetter(part, whole):
return (100 * int(part)/int(whole))
for x in percents:
percentList.append(int(percentageFinder(x,domainAmount)))
percentL = percentList
#End code for percentage finding
for firstdomain in case_list:
for domain in firstdomain:
dnsResolver(domain)
if dnsResolver != "Did Not Resolve":
rd += 1
else:
dnresolve += 1
# Using w+ to overwrite all Domain Names &
with open(dnsList,'w+', newline="") as r:
writers = csv.writer(r)
writers.writerows(dns_list)
# This is used for showing the percentage of the matching you have done
part += 1
if part in percentL:
total = int(percentGetter(part, domainAmount))
print("I Have Resolved {}".format(total) + "%" + " Domains:")
else:
pass
print("Resolving has completed. Statistics Below:")
print("------------------------------------------")
print("You had " + str(rd) + " domains that resolved.")
print("You had " + str(dnresolve) + " domains that did NOT resolve")
# THIS IS THE END OF THE WORKING CODE - LABELED AS #2
# Write the dns Resolver names into a new csv file
print("Now writing your domains & their DNS Name to an Output File:")
with open(outputFile,'w+', newline="\n") as f:
writer = csv.writer(f, dialect='excel')
writer.writerow(fields)
for row in csv_output:
writer.writerow(row)
print("Writing a backup CSV File")
# Using this to create a backup in case to contain all domains, and all resolvers
# If someone runs the script with a small list of domains, still want to keep a
# running list of everything in case any questions arise.
# This is done by using 'a' instead of 'w' or 'w+' done above.
with open(backupCSV,'w', newline="") as f:
writer = csv.writer(f, dialect='excel')
writer.writerow(fields)
for row in csv_output:
writer.writerow(row)
print("Your backup is now done processing. Exiting program")
# Sort the files by keyword, in this case the domain being azure or csc
for r in dns_list:
if keyAzure in r:
for x in keyAzure:
FileName = x
print(FileName)
exit()
I wrote some code to extract email and IP addresses from bulk text. However, the code extracts only the email addresses. (The original text, which I would like to make understandable, is a typical log file). I don't know why the generated file does not give me back the IP addresses.
import os
import re
# 1
filename = 'errors.txt'
newfilename = 'emaillist-rev.txt'
# 2
if os.path.exists(filename):
data = open(filename,'r')
bulkemails = data.read()
else:
print "File not found."
raise SystemExit
# 3
r = re.compile(r'[\w\.-]+#[\w\.-]+')
results = r.findall(bulkemails)
emails = ""
for x in results:
emails += str(x)+"\n"
# 4
ip = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
result = ip.findall(bulkemails)
ip =""
for y in result:
ip += str(y)+"\n"
# 5
def writefile():
f = open(newfilename, 'w')
f.write(emails + ip)
f.close()
print "File written."
# 6
def overwrite_ok():
response = raw_input("Are you sure you want to overwrite "+str(newfilename)+"? Yes or No\n")
if response == "Yes":
writefile()
elif response == "No":
print "Aborted."
else:
print "Please enter Yes or No."
overwrite_ok()
# 7
if os.path.exists(newfilename):
overwrite_ok()
else:
writefile()
When declaring the ip regex, replace the anchors with word boundaries and mind you need to use a raw string literal.
ip = re.compile(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b')
I have a text file called sample_ips.txt containing some random IP addresses as follows:-
182.0.0.15
182.0.0.16
182.0.0.17
I am giving an IP address as an input to check if that IP exist in the text file it prints true else false.
Here is my code snippet:-
ip_input = str(input("Enter IP:"))
ip = open("sample_ips", "r")
data = ip.readlines()
for ips in data:
ips = ips.strip("\n")
if ip_input in ips:
print "true"
else:
print "false"
It's throwing a syntax error
File "<string>", line 1
182.0.0.15
^
SyntaxError: invalid sytanx
I think it is not able to take the input as a string despite me declaring it as a string in my code. Any help?
Thanks
input_ip = raw_input("Enter IP:") # In python 2.x. If you use input() then type your inputs as string (>>Enter IP:"182.0.0.15")
#input_ip = input("Enter IP:") for python 3.x
with open("ip.txt", "r") as ip:
data = ip.readlines()
for ips in data:
ips = ips.strip("\n")
if input_ip in ips:
print ("true")
else:
print ("false")
Well, this works:
input = raw_input("Enter IP:")
ip = open("sample_ips.txt", "r")
data = ip.readlines()
for ips in data:
ips = ips.strip("\n")
if input in ips:
print "true"
else:
print "false"
:edit:
Python3 version
input = input("Enter IP:")
ip = open("sample_ips.txt", "r")
data = ip.readlines()
for ips in data:
ips = ips.strip("\n")
if input in ips:
print ("true")
else:
print ("false")