473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

File reading across network (windows)

Hello,

Suppose I have a Vista machine called VISTA and an XP machine called
XP in a workgroup named WORKGROUP. Physically they're connected to a
router and I can see lists of public and shared files on each of them.
How do I address these for IO?

A search suggested that the form open(r"\\server\folder\folder"), but
I tried many combinations guessing what it wants for that path in my
case (r"\\WORKGROUP\VISTA", "\\VISTA\PUBLIC", etc), and none have
worked so far.

Thanks!
Aug 12 '08 #1
4 16054
Prof. William Battersea schrieb:
Hello,

Suppose I have a Vista machine called VISTA and an XP machine called
XP in a workgroup named WORKGROUP. Physically they're connected to a
router and I can see lists of public and shared files on each of them.
How do I address these for IO?

A search suggested that the form open(r"\\server\folder\folder"), but
I tried many combinations guessing what it wants for that path in my
case (r"\\WORKGROUP\VISTA", "\\VISTA\PUBLIC", etc), and none have
worked so far.
You need to create network shares in windows first. Once these are
established, *all* programs using file-IO - including python - can
access files.

Diez
Aug 12 '08 #2
On Aug 12, 8:48 am, "Prof. William Battersea"
<williambatter...@gmail.comwrote:
Hello,

Suppose I have a Vista machine called VISTA and an XP machine called
XP in a workgroup named WORKGROUP. Physically they're connected to a
router and I can see lists of public and shared files on each of them.
How do I address these for IO?

A search suggested that the form open(r"\\server\folder\folder"), but
I tried many combinations guessing what it wants for that path in my
case (r"\\WORKGROUP\VISTA", "\\VISTA\PUBLIC", etc), and none have
worked so far.

Thanks!
Does the remote folder require authentication? Maybe you could map a
network drive and then access files using (z:/VISTA-FOLDER/ )
Aug 12 '08 #3
"Diez B. Roggisch" <de***@nospam.web.dewrote:
>A search suggested that the form open(r"\\server\folder\folder"), but
I tried many combinations guessing what it wants for that path in my
case (r"\\WORKGROUP\VISTA", "\\VISTA\PUBLIC", etc), and none have
worked so far.
The combination you want is \\server\sharename\folder\filename, so if
the Vista machine is called VISTA and has a folder shared as PUBLIC then
"\\VISTA\PUBLIC" might work. But several things can go wrong with this.
>
You need to create network shares in windows first. Once these are
established, *all* programs using file-IO - including python - can
access files.
It isn't absolutely essential to create a network share first: most
windows apis, including those used by Python, will be very happy with a
UNC path:
>>open(r'\\10.10.25.121\rootc\default.tag', 'r').read()
'\tTAGS\tUNSORTED\n'

The first problem you need to address is whether or not the two systems
know each other's names. This can be problematic: that's why I used an
IP address in the example I gave. If you don't have a domain controller
on the network then the machines which are on the network use a complex
system of voting to decide which will be the 'browse master', but they
don't always agree on the outcome :^)

The second problem is that if you don't establish a connection first,
you cannot specify a username and password when opening the file. That
means you get whatever the default is. Again not usually a problem if
your machines are in a domain, but in a workgroup it can be messy.

So while it is not essential, it is probably easier to establish a
connection. Of course that can be done fairly easily from within
Pythonusing win32api.NetUseAdd, ctypes, or even os.system("NET USE
\\server\share /user:yourname pa5sW0rc!")

Note that you don't need to assign a drive letter when creating the
connection, just get the authentication over with and then you can use a
UNC name for the the actual file access.

----- netuse.py -------
from __future__ import with_statement
import ctypes
from ctypes import *
from ctypes.wintypes import *
import getpass
import os

def _stdcall(dllname, restype, funcname, *argtypes):
# a decorator for a generator.
# The decorator loads the specified dll, retrieves the
# function with the specified name, set its restype and argtypes,
# it then invokes the generator which must yield twice: the first
# time it should yield the argument tuple to be passed to the dll
# function (and the yield returns the result of the call).
# It should then yield the result to be returned from the
# function call.
def decorate(func):
api = getattr(WinDLL(dllname), funcname)
api.restype = restype
api.argtypes = argtypes

def decorated(*args, **kw):
iterator = func(*args, **kw)
nargs = iterator.next()
if not isinstance(nargs, tuple):
nargs = (nargs,)
try:
res = api(*nargs)
except Exception, e:
return iterator.throw(e)
return iterator.send(res)
return decorated
return decorate

# Structure for NetUseAdd:
class USE_INFO_2(Structure):
_fields_ = [("local", LPWSTR),
("remote", LPWSTR),
("password", LPWSTR),
("status", DWORD),
("asg_type", DWORD),
("refcount", DWORD),
("usecount", DWORD),
("username", LPWSTR),
("domain", LPWSTR)]

@_stdcall("netapi32", c_int, "NetUseAdd", LPWSTR, DWORD, POINTER(USE_INFO_2), POINTER(DWORD))
def NetUseAdd(local, remote, password, asg_type=0, username=None, domainname=None):
"""Add a network connection. Returns 0 if successful"""
useinfo = USE_INFO_2(local, remote, password, 0, 0, 0, 0, username, domainname)
ParmError = DWORD()
res = yield(None, 2, useinfo, ParmError)
if res != 0:
raise RuntimeError("Could not create connection", res)
yield res

@_stdcall("netapi32", c_int, "NetUseDel", LPWSTR, LPWSTR, DWORD)
def NetUseDel(usename, forcecond=0):
res = yield(None, usename, forcecond)
if res != 0:
raise RuntimeError("Could not delete connection", res)
yield res

if __name__=='__main__':
username = "ZZZZZZ"
password = getpass.getpass("Password:")
sharename = r"\\10.10.25.121\rootc"
filename = "default.tag"
try:
with open(os.path.join(sharename, filename), "r") as f:
print f.read()
except IOError, e:
print "without net use, got", e
else:
raise RuntimeError("We connected without doing the net use!")

NetUseAdd(None, sharename, password, username=username)
with open(os.path.join(sharename, "default.tag"), "r") as f:
print f.read()
NetUseDel(sharename)

-----------------------

--
Duncan Booth http://kupuguy.blogspot.com
Aug 12 '08 #4
>You need to create network shares in windows first. Once these are
>established, *all* programs using file-IO - including python - can
access files.
It isn't absolutely essential to create a network share first: most
windows apis, including those used by Python, will be very happy with a
UNC path:
<snip/>

You still need to create a share on the serving machine. The UNC-path is
just for accessing it without *mounting* it on a local machine under some
drive-letter. I should have worded clearer what I meant, though.

You are of course right that actually mounting the drives makes it much
easier to deal with them regarding authentication.

Diez
Aug 12 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SR | last post by:
Hi Folks, I have a quick question regarding the possibility of extracting specific information from a text file, then performing calculations (average delay/jitter/throughput) based on that...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
11
by: Andre | last post by:
Hi, I have ASP.NET application running on standalone (not part of the domain) Windows 2003. I use forms authentication for my application. The problem I have is that I need to create and read...
4
by: mabond | last post by:
Hi I have a number of apps which are compiled to a shared network directory. Users run the executables from this directory. When rebuilding my apps I cannot complete the build if the...
3
by: Parag Gaikwad | last post by:
Hi, I need to delete files across the network using web client (IE 6.x) in Win 2003 - IIS 6.0 environment. Can someone please suggest an approach I can use to acheive this. Will using FSO do...
1
by: Thomas Kowalski | last post by:
Hi, I have to parse a plain, ascii text file (on local HD). Since the file might be many millions lines long I want to improve the efficiency of my parsing process. The resulting data structure...
5
by: =?Utf-8?B?QWRyaWFuTW9ycmlz?= | last post by:
Hello! I'm trying to copy a file from another computer on the network that I do not have permission with my current logon details to access. If I open the folder using the Windows file manager...
0
by: Gabriel Genellina | last post by:
En Tue, 12 Aug 2008 00:48:21 -0300, Prof. William Battersea <williambattersea@gmail.comescribi�: This is mostly a network administrative problem... Both machines are in the SAME workgroup,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.