Python Find IP and upload to FTP - python

I've been trying to write a simple script to grab my ip and upload it to an ftp server. I can do it if i use just use a simple string but im struggling at uploading the IP address. Im thinking something like
def testftp():
filename="testing1.txt" #know this should all be indented
targetfile=open(filename, 'a')
#get my ip, dont think this is the way to do it, but it works in interactive anyways
getmyip=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
getmyip.connect(("google.com", 80))
print(getmyip.getsockname()[0])
targetfile.write("Your ip address is: ")
targetfile.write(getip)
tragetfile.write('\n')
session = ftplib.FTP('ftp.com', 'user', 'pass')
file = open('testing1.txt', 'rb')
session.storbinary('STOR testing1.txt', file)
file.close()
session.quit()
testftp()
I get an "TypeError: expected a character buffer object" error and i've googled that but i cant seem to find anything specific to this example. ehh. So i hit a brick wall and thought id finally post here. Hopefully someone can help.. Thanks in advance.

Related

how to set the necessary permission for my code to use?

I'm writing a code for packet sniffing in python and in windows 10. According to my searching, I saw that most of the developers said that Linux is better for packet sniffing but I can't use Linux right now, So how could i fix this error?
I tried using print(ctypes.windll.shell32.IsUserAnAdmin()) and it gave 0 value.
This is my code:
def main():
print(ctypes.windll.shell32.IsUserAnAdmin())
conn = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_IP)
while True:
raw_data, addr = conn.recvfrom(65535)
dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
print("\nEthernet Frame:")
print("Destination: {}, Source: {}, Protocol: {}".format(dest_mac,src_mac,eth_proto))
And I've got this error:
OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
I know it's something about permission but how can i do that? Thank you in advance :)

utf-8 issue when using socket in server PC

I want to send some file from a client PC to a server PC using Python.
But when I run the client code, there is a problem.
Here is the client code:
with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as sock:
sock.connect((HOST,PORT))
sock.sendall(filename.encode('utf-8'))
print(filename.encode('utf-8'))
and this is the server code:
filename = self.request.recv(1024)
print(filename)
filename = filename.decode('utf-8')
When I run it within my PC (for example, server=client localhost), it runs perfectly.
But in my server PC, print result has some added text like:
b'filename\~~~~~~~~~~'
filename result
error message
So I think if the added text is gone when I use my server PC, the problem will be solved.
But I dont know how to get there.
Please let me know...

receiveing "550 data channel timed out" while using python storbinary

I am trying to upload files from a UNIX server to a windows server using python FTP_TLS. Here is my code:
from ftplib import FTP_TLS
ftps = FTP_TLS('server')
ftps.connect(port=myport)
ftps.login('user', 'password')
ftps.prot_p()
ftps.retrlines('LIST')
remote_path = "MYremotePath"
ftps.cwd(remote_path)
ftps.storbinary('STOR myfile.txt', open('myfile.txt', 'rb'))
myfile.close()
ftps.close()
I can connect to the server successfully and receive the list of files, but I am not able able to upload the file and after some time I receive the following error:
ftplib.error_perm: 550 Data channel timed out due to not meeting the minimum bandwidth requirement.
I should mention that I am able to upload files on the same server using perl FTPSSL library. The problem happens only in python.
Does anyone have any idea about how to resolve this issue?
Thanks

Windows alternative of this python script for linux to get IP from URL

I am very new to Python. So in newboston's latest python tutorial Im making a web crawler. This Python script however only works on Linux. What would be the windows alternative to the following code?
import os
def get_ip_address(url):
command = "host " + url
process = os.popen(command)
results = str(process.read())
marker = results.find('has address') + 12
return results[marker:].splitlines()[0]
print(get_ip_address('google.com'))
I want it in the same format as I want to code multiple modules(i.e. get ip, who is etc.) and put them together as one Python program.
I am trying to obtain ONLY the ip address and nothing else in the results.
Alternatively, This is what I have for Windows so far:
import socket
def get_ips_for_host(url):
try:
ips = socket.gethostbyname_ex(url)
except socket.gaierror:
ips = []
return ips
ips = get_ips_for_host('www.facebook.com')
print(repr(ips))
How can i modify it to make it get only the IP address?
Similarly how can I get the nmap scan and the 'who is'?
the_ip = get_ips_for_host('www.facebook.com')[-1][0]

sending a period to end a mail DATA telnet session in python

im using a telnetlib in python to connect to a simple mail server and send out an email; everything works fine until the very end after i enter the DATA command, you submit the body of your message and in order to submit the email in the server's queue, you need to type a period "." on a new line by itself so my end transaction snippet looks like so
self.tnet.write("\n.\n")
self.tnet.read_until("250")
where self.tnet is my telnet session var and read_until waits for the 250 Ok response saying the email has been sent to the queue of the email server and on its way for delivery; however, i do not get that 250 Ok response back and the connection times out to my 10sec timeout flag and no email is received in my inbox... any ideas? ive also tried
self.tnet.write(raw_input()+"\n")
and ive also tried grabbing the raw socket
self.sock=self.tnet.get_socket()
self.sock.send(".\n")
print self.sock.recv(1024)
no response... :/
ive also tried the return carriage "\r" in combination with "\n" and on it own to no avail
any ideas?
thank you,
~george
you need the <CR><LF> newline-squence try:
self.tnet.write("\r\n.\r\n")
hope that helps

Categories

Resources